如何修复Selenium无法找到元素的错误

huangapple go评论55阅读模式
英文:

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

&lt;span id=&quot;ctl00_PlaceHolderMain_lblGSLoadASI&quot;&gt;Search Additional Criteria&lt;/span&gt;

which can be located by its ID,

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, &quot;ctl00_PlaceHolderMain_lblGSLoadASI&quot;))).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(&quot;https://permits.mynevadacounty.com/CitizenAccess/Cap/CapHome.aspx?module=Building&amp;TabName=Building&quot;)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, &quot;img[src=&#39;/CitizenAccess/nc-horizontal.png&#39;]&quot;)))
wait.until(EC.element_to_be_clickable((By.ID, &quot;ctl00_PlaceHolderMain_lblGSLoadASI&quot;))).click()

driver.quit()

huangapple
  • 本文由 发表于 2023年4月20日 07:48:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定