无法在 Selenium 中找到元素 Python
Cant Find Element in Selenium Python
您好,我正在尝试使用 selenium 查找要单击的按钮。下面是我正在使用的 HTML 代码片段。
<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
我试图用下面的代码点击运行按钮。
elem = driver.find_element_by_id('runButton').click()
我收到以下错误消息:
NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
不确定还可以尝试什么。
很可能您需要使用等待来找到您的元素。在与元素交互之前,您需要留出时间让元素可见、可点击等。您可以在此处找到有关等待的信息:https://selenium-python.readthedocs.io/waits.html
摘自上述网站:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
如果等待不起作用,则您的元素可能位于 iframe 中。您需要先切换到该 iframe,然后搜索该元素才能找到它。
您会像找到另一个元素一样找到 iframe,然后像这样切换到它:
iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)
button = driver.find_element_by_id("runButton")
button.click()
使用完 iframe 及其内容后,您需要切换回来:
driver.switch_to.default_content()
该元素似乎是一个动态元素,所以要 click()
在您需要使用 element_to_be_clickable()
的元素上,您可以使用以下任一项 :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#runButton[value='Run Report']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='button' and @id='runButton'][@value='Run Report']"))).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 查找要单击的按钮。下面是我正在使用的 HTML 代码片段。
<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
我试图用下面的代码点击运行按钮。
elem = driver.find_element_by_id('runButton').click()
我收到以下错误消息:
NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
不确定还可以尝试什么。
很可能您需要使用等待来找到您的元素。在与元素交互之前,您需要留出时间让元素可见、可点击等。您可以在此处找到有关等待的信息:https://selenium-python.readthedocs.io/waits.html
摘自上述网站:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
如果等待不起作用,则您的元素可能位于 iframe 中。您需要先切换到该 iframe,然后搜索该元素才能找到它。
您会像找到另一个元素一样找到 iframe,然后像这样切换到它:
iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)
button = driver.find_element_by_id("runButton")
button.click()
使用完 iframe 及其内容后,您需要切换回来:
driver.switch_to.default_content()
该元素似乎是一个动态元素,所以要 click()
在您需要使用 element_to_be_clickable()
的元素上,您可以使用以下任一项
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#runButton[value='Run Report']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='button' and @id='runButton'][@value='Run Report']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC