英文:
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
- 浏览器快照:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论