使用 Selenium 通过自定义按钮属性查找元素
Finding element by custom button attribute using Selenium
在网页上,我有一个具有各种属性的按钮元素。
<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>
我尝试使用具有不同语法形式的 selenium select 并使用其 button-text
属性单击该元素,但我不确定错误来自何处。肯定不是时间问题,也就是说,页面是在执行find命令的时候加载的
from selenium import webdriver
driver = webdriver.Chrome()
import time
driver.get ('https://mywebsite.com')
#time.sleep(10)
driver.find_element_by_css_selector(".button[button-text='Check for new tables and columns']").click()
我也试过驼峰式 buttonText
和 button_text
代替 button-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_selector
:
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()
所需的元素是动态元素,因此理想情况下,单击需要为 的元素50=] 并且您可以使用以下任一项 :
使用CSS_SELECTOR
:
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 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>
我尝试使用具有不同语法形式的 selenium select 并使用其 button-text
属性单击该元素,但我不确定错误来自何处。肯定不是时间问题,也就是说,页面是在执行find命令的时候加载的
from selenium import webdriver
driver = webdriver.Chrome()
import time
driver.get ('https://mywebsite.com')
#time.sleep(10)
driver.find_element_by_css_selector(".button[button-text='Check for new tables and columns']").click()
我也试过驼峰式 buttonText
和 button_text
代替 button-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_selector
: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_SELECTOR
: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