使用 Python 和 Selenium 按文本单击按钮
Click button by text using Python and Selenium
是否可以用Selenium点击多个具有相同文本的按钮?
您可以通过文本找到所有按钮,然后在 for
循环中为每个按钮执行 click()
方法。
使用这个 SO answer 它将是这样的:
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
for btn in buttons:
btn.click()
我还建议您看一下 Splinter,这是一个很好的 Selenium 包装器。
Splinter is an abstraction layer on top of existing browser automation
tools such as Selenium, PhantomJS and zope.testbrowser. It has a
high-level API that makes it easy to write automated tests of web
applications.
我在 html 中有以下内容:
driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
@nobodyskiddy,尝试使用driver.find_element
(如果你有一个单一的按钮选项),当你使用driver.find_elements
时使用click()
的索引,find_elements
将return array to web elements values, 所以你必须使用索引 select 或者点击
要通过文本定位并单击 <button>
元素,您可以使用以下任一方法 :
使用 xpath 和 text()
:
driver.find_element_by_xpath("//button[text()='button_text']").click()
使用 xpath 和 contains()
:
driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
理想情况下,要通过文本定位并单击 <button>
元素,您需要为 element_to_be_clickable()
引入 WebDriverWait,您可以使用以下 :
使用 XPATH 和 text()
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()
使用 XPATH 和 contains()
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).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>
元素,您可以使用以下任一方法 :
使用 xpath 和 text()
:
for button in driver.find_elements_by_xpath("//button[text()='button_text']"):
button.click()
使用 xpath 和 contains()
:
for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"):
button.click()
理想情况下,要通过文本定位所有 <button>
元素,您需要为 visibility_of_all_elements_located()
引入 WebDriverWait,您可以使用以下任一方法:
使用 XPATH 和 text()
:
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))):
button.click()
使用 XPATH 和 contains()
:
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))):
button.click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
是否可以用Selenium点击多个具有相同文本的按钮?
您可以通过文本找到所有按钮,然后在 for
循环中为每个按钮执行 click()
方法。
使用这个 SO answer 它将是这样的:
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
for btn in buttons:
btn.click()
我还建议您看一下 Splinter,这是一个很好的 Selenium 包装器。
Splinter is an abstraction layer on top of existing browser automation tools such as Selenium, PhantomJS and zope.testbrowser. It has a high-level API that makes it easy to write automated tests of web applications.
我在 html 中有以下内容:
driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
@nobodyskiddy,尝试使用driver.find_element
(如果你有一个单一的按钮选项),当你使用driver.find_elements
时使用click()
的索引,find_elements
将return array to web elements values, 所以你必须使用索引 select 或者点击
要通过文本定位并单击 <button>
元素,您可以使用以下任一方法
使用 xpath 和
text()
:driver.find_element_by_xpath("//button[text()='button_text']").click()
使用 xpath 和
contains()
:driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
理想情况下,要通过文本定位并单击 <button>
元素,您需要为 element_to_be_clickable()
引入 WebDriverWait,您可以使用以下
使用 XPATH 和
text()
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()
使用 XPATH 和
contains()
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).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>
元素,您可以使用以下任一方法
使用 xpath 和
text()
:for button in driver.find_elements_by_xpath("//button[text()='button_text']"): button.click()
使用 xpath 和
contains()
:for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"): button.click()
理想情况下,要通过文本定位所有 <button>
元素,您需要为 visibility_of_all_elements_located()
引入 WebDriverWait,您可以使用以下任一方法
使用 XPATH 和
text()
:for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))): button.click()
使用 XPATH 和
contains()
:for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))): button.click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC