Selenium无法通过XPath找到元素,即使XPath是正确的。

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

selenium cannot find element by xpath even though the xpath is correct

问题

在这个网站https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000上,我试图使用以下代码向名字字段发送按键:

    driver5 = webdriver.Chrome(options=chrome_options)
    driver5.get('https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000')
    #fnbo first name
    fnbofname = driver5.find_element(By.XPATH, '//*[@id="firstName"]')
    fnbofname.send_keys(fname)

但我却收到了如下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=\"firstName\"]"}

我注意到网站上有一些iframes,但我不知道它们是否会影响我的代码,因此我在这里问一下是否有人可以给我一些指示/示例,因为我感觉我一直遇到的问题可能是由iframes引起的,但我不知道如何处理它们,因为它们并不直接出现在我尝试复制的HTML文本中。
英文:

on this website https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000 i am trying to send keys to the first name field using this code

driver5 = webdriver.Chrome(options=chrome_options)
driver5.get('https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000')
#fnbo first name
fnbofname = driver5.find_element(By.XPATH, '//*[@id="firstName"]')
fnbofname.send_keys(fname)

but i instead get this error returned

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="firstName"]"}

i did notice there was a couple iframes on the site but i dont know if they would affect my code hence why i am asking here if anyone can give me pointers/ examples as i feel like i keep running into what i think are problems cause by iframes but i dont know how to deal with them since they are not directly in the html text i am trying to copy

答案1

得分: 1

要的元素是一个 Angular 元素,因此理想情况下,要向元素发送一个 字符序列,您需要使用 WebDriverWait 来等待 element_to_be_clickable(),您可以使用以下任一 定位策略

  • 使用 CSS_SELECTOR
driver.get("https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#firstName"))).send_keys("thrvxx")
  • 使用 XPATH
driver.get("https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='firstName']"))).send_keys("thrvxx")
  • 注意:您必须添加以下导入语句:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

Selenium无法通过XPath找到元素,即使XPath是正确的。

英文:

The desired element is a Angular element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#firstName"))).send_keys("thrvxx")
    
  • Using XPATH:

    driver.get("https://www.firstbankcard.com/lynx/#/pre-qual/intake?agent=H48&sub=000")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='firstName']"))).send_keys("thrvxx")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser snapshot:

Selenium无法通过XPath找到元素,即使XPath是正确的。

huangapple
  • 本文由 发表于 2023年3月12日 12:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711124.html
匿名

发表评论

匿名网友

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

确定