简体   繁体   English

浏览器更改时,Python Selenium无法找到元素

[英]Python Selenium can't find elements when browser changes

I'm writing a script to auto fill some fields with Selenium. 我正在编写一个脚本,以使用Selenium自动填充某些字段。 I'm running into problems where if the script clicks on a button that navigates from the original url, then Python can't find the elements on the new page. 我遇到了一个问题,如果脚本单击从原始URL导航的按钮,那么Python将无法在新页面上找到元素。

For example: If I have selenium navigate to this landing page first ( https://www.emedny.org ) and click on the log in for ePaces, and then try to fill out the form, I get an error. 例如:如果我有硒,请先导航到该登录页面( https://www.emedny.org ),然后单击登录以获取ePaces,然后尝试填写表格,我会收到一条错误消息。 This code is what I run: 这段代码是我运行的:

from selenium import webdriver

browser = webdriver.Chrome(path)

keyed_path = r'https://www.emedny.org'
browser.get(keyed_path)

epaces_login = browser.find_element_by_css_selector('#targetContainer > a:nth-child(3)')
epaces_login.click()

username = browser.find_element_by_id('Username')
username.send_keys('test')

This code opens up https://www.emedny.org , clicks on a button which takes us to a log in page ( https://www.emedny.org/epaces ), and is supposed to enter in a username on the page. 此代码打开https://www.emedny.org ,单击一个按钮,该按钮会将我们带到登录页面( https://www.emedny.org/epaces ),并在页。 However, I get this error: 但是,我收到此错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Username"}
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.3.9600 x86_64)

But, if I skip the landing page and open up the log in page directly, like in this code: 但是,如果我跳过登录页面并直接打开登录页面,如以下代码所示:

from selenium import webdriver

browser = webdriver.Chrome(path)

keyed_path = r'https://www.emedny.org/epaces'
browser.get(keyed_path)

username = browser.find_element_by_id('Username')
username.send_keys('test')

Then the code runs well, and updates the username field as expected. 然后,代码运行良好,并按预期更新用户名字段。 It seems as though Selenium might be searching for that id on the opening url only . 似乎Selenium可能在开头url上搜索该id。

Here's the HTML for the username field: 这是用户名字段的HTML:

<input name="Username" type="text" maxlength="8" id="Username" class="inputText" size="13" onchange="javascript:validate(this)">

Any suggestions? 有什么建议么? I've tried id, XPath, css selector, name, etc. Same error each time. 我试过了id,XPath,css选择器,名称等。每次都出现相同的错误。

EDIT: The link that selenium clicks first opens up a new tab. 编辑:硒首先单击的链接打开一个新选项卡。 Could that be part of the issue? 这可能是问题的一部分吗?

I figured it out. 我想到了。 It's because the link being clicked on opened up a new tab. 这是因为单击链接会打开一个新选项卡。 Selenium was searching for the elements on the previous tab. 硒正在上一个选项卡上搜索元素。 I used window_handles and switch_to_window like this: 我这样使用window_handlesswitch_to_window

browser = webdriver.Chrome(path)


keyed_path = r'https://www.emedny.org'
browser.get(keyed_path)

print(browser.window_handles)

epaces_login = browser.find_element_by_css_selector('#targetContainer > a:nth-child(3)')
epaces_login.click()


browser.switch_to_window(browser.window_handles[1])

username = browser.find_element_by_id('Username')

username.send_keys('test')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM