How to locate the button element using Selenium through Python

落花浮王杯 提交于 2020-02-12 05:47:07

问题


I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.

HTML:


回答1:


To locate and click() on the element with text as Not Now you can use the following Locator Strategy:

  • Using xpath:

    driver.find_element_by_xpath("//button[text()='Not Now']").click()
    

However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:

  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Reference

You can find a couple of relevant discussions in:

  • What does contains(., 'some text') refers to within xpath used in Selenium
  • While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
  • How does dot(.) in xpath to take multiple form in identifying an element and matching a text


来源:https://stackoverflow.com/questions/59796113/how-to-locate-the-button-element-using-selenium-through-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!