英文:
How to fix Selenium unable to find element error
问题
我无法在我的Python脚本中使用Selenium定位一个元素,并且我无法弄清楚为什么它无法在这个网页中找到该元素。我尝试了两种方法,一种是常规方法,另一种是等待页面加载,但两者都没有成功。第一种方法显示了以下错误:"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="btnGSExpandCollapse"]"}",而异步方法则简单地超时了。以下是代码
第一种方法:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
expand_button = driver.find_element(By.ID, "btnGSExpandCollapse")
expand_button.click()
第二种方法:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "btnGSExpandCollapse"))
)
finally:
driver.quit()
英文:
I am having issues locating an element with selenium in my python script and I cannot figure out why it is unable to locate the element in this webpage. I tried two approaches one regular and another that waits for the page to load and neither worked with the first displaying this error : "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="btnGSExpandCollapse"]"}" and the asynchronous approach simply timing out. Here is the code
first approach:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
expand_button = driver.find_element(By.ID, "btnGSExpandCollapse")
expand_button.click()
second approach:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "btnGSExpandCollapse"))
)
finally:
driver.quit()
答案1
得分: 1
以下是翻译好的内容:
在您点击“搜索其他条件”链接之后,您要查找的元素实际上并不存在于页面上。当页面首次加载时,您要查找的内容是:
<span id="ctl00_PlaceHolderMain_lblGSLoadASI">Search Additional Criteria</span>
可以通过其ID找到:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "ctl00_PlaceHolderMain_lblGSLoadASI"))).click()
注意:当等待要点击的元素时,请使用 EC.element_to_be_clickable()
。如果要与元素交互,例如 .text
等,请使用 EC.visibility_of_element_located()
。"Presence" 只表示元素在DOM中存在,而不表示它可交互。在加载速度较慢的页面上,等待元素出现然后与之交互或点击可能会导致错误。这在大多数情况下可能不会发生,但有一定几率。
以下是工作代码示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[src='/CitizenAccess/nc-horizontal.png']")))
wait.until(EC.element_to_be_clickable((By.ID, "ctl00_PlaceHolderMain_lblGSLoadASI"))).click()
driver.quit()
英文:
The element you are looking for doesn't actually exist on the page until AFTER you click the "Search Additional Critera" link. What you are looking for when the page first loads is
<span id="ctl00_PlaceHolderMain_lblGSLoadASI">Search Additional Criteria</span>
which can be located by its ID,
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "ctl00_PlaceHolderMain_lblGSLoadASI"))).click()
NOTE: When you are waiting for an element that you are going to click, use EC.element_to_be_clickable()
. If you are going to interact with an element, e.g. .text
, etc. then use EC.visibility_of_element_located()
. Presence just means that the element is in the DOM, not that it's interactable. You might get lucky most of the time but there's a chance that if you wait for present and then interact with or click an element you'll get an error. It happens more on pages that load slowly.
The working code looks like
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&TabName=Building")
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[src='/CitizenAccess/nc-horizontal.png']")))
wait.until(EC.element_to_be_clickable((By.ID, "ctl00_PlaceHolderMain_lblGSLoadASI"))).click()
driver.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论