【问题标题】:How to locate the button element using Selenium Webdriver?如何使用 Selenium Webdriver 定位按钮元素?
【发布时间】:2019-08-31 22:43:11
【问题描述】:
我在定位按钮并单击它时遇到问题。它在 Internet Explorer 中弹出。我按类名使用,但不起作用。
browser.find_element_by_css_selector("ui-button ui-corner-all ui-widget").click()
HTML:
【问题讨论】:
标签:
python-3.x
selenium-webdriver
xpath
css-selectors
webdriverwait
【解决方案1】:
要定位并单击文本为 Accept 的元素,您需要诱导 WebDriverWait 以使 元素可单击,您可以使用以下Locator Strategies:
-
使用CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ui-dialog-buttonset>button.ui-button.ui-corner-all.ui-widget"))).click()
-
使用XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='ui-dialog-buttonset']//button[@class='ui-button ui-corner-all ui-widget' and text()='Accept']"))).click()
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC