Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Selenium通过自定义按钮属性查找元素_Python_Selenium Webdriver_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 使用Selenium通过自定义按钮属性查找元素

Python 使用Selenium通过自定义按钮属性查找元素,python,selenium-webdriver,xpath,css-selectors,webdriverwait,Python,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,在网页上,我有一个具有各种属性的按钮元素 <button class="alert-area rj-button--plain ng-binding" ng-click="forceStructSync()" bs-tooltip="" data-original-title="This runs a structure sync, and makes sure our warehouse is aware of any

在网页上,我有一个具有各种属性的按钮元素

<button class="alert-area rj-button--plain ng-binding" ng-click="forceStructSync()" bs-tooltip="" data-original-title="This runs a structure sync, and makes sure our warehouse is aware of any new tables you've added to your database recently." data-placement="right" button-text="Check for new tables and columns"> <span class="rj-icon--refresh"></span> Check for new tables and columns </button>
我还尝试了驼峰式的
buttonext
button\u text
,但这会引发相同的错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to lo
cate element: {"method":"css selector","selector":".button[button-text='Check for new tabl
es and columns']"}

关于这里的正确语法有什么建议吗?

实际上,答案是不包括按钮前的句点,所以它起作用了:

driver.find_element_by_css_selector("button[button-text='Check for new tables and columns']").click()

要单击图元,可以使用以下任一选项:

  • 使用
    css\u选择器

    driver.find_element(By.CSS_SELECTOR, "button.alert-area.rj-button--plain.ng-binding[ng-click^='forceStructSync'][data-original-title^='This runs a structure sync']").click()
    
  • 使用
    xpath

    driver.find_element(By.XPATH, "//button[@class='alert-area rj-button--plain ng-binding' and starts-with(@ng-click, 'forceStructSync')][contains(., 'Check for new tables and columns')]").click()
    

所需的元素是一个动态元素,因此理想情况下,单击需要为导入的元素,可以使用以下任一选项:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.alert-area.rj-button--plain.ng-binding[ng-click^='forceStructSync'][data-original-title^='This runs a structure sync']"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='alert-area rj-button--plain ng-binding' and starts-with(@ng-click, 'forceStructSync')][contains(., 'Check for new tables and columns')]"))).click()
    
  • 注意:您必须添加以下导入:

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

表示一个类别。在本例中,BUTTON是一个标记,而不是一个类。这就是移除
修复此定位器的原因。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC