使用id(x)无法通过id或xpath定位按钮元素。元素似乎具有多个ID名称

时间:2019-01-21 09:15:13

标签: python selenium

我一直试图通过ID,xpath,链接文本和部分链接文本来定位按钮,但是每次都会出现“无法定位元素”错误。

元素的html如下:

<a href="https://s1.ebridge.com/ebridge/3.0/retrieve/search.aspx?search=new&amp;guid=14f2de4c-b37a-4632-a593-48d8aa12b468" id="aView" onclick="clickme(this,'retrieve')" target="main">Retrieve</a>

该页面所在的页面位于登录屏幕后面,我可以顺利通过该页面而没有任何问题。这是我从头到尾的代码:

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> from bs4 import BeautifulSoup
>>> import re
>>> import pandas as pd
>>> import os
>>> url = "https://s1.ebridge.com/ebridge/3.0/default.aspx?1"
>>> driver = webdriver.Firefox(executable_path="/Applications/Postgres.app/Contents/Versions/11/bin/geckodriver")
>>> driver.implicitly_wait(30)
>>> driver.get(url)
>>> username = driver.find_element_by_name("tbUserName")
>>> username.clear()
>>> username.send_keys("public")
>>> password = driver.find_element_by_name("tbPassword")
>>> password.clear()
>>> password.send_keys("public")
>>> file_cabinet = driver.find_element_by_name("tbFileCabinet")
>>> file_cabinet.clear()
>>> file_cabinet.send_keys("PINCHD")
>>> file_cabinet.send_keys(Keys.RETURN)
>>> driver.implicitly_wait(30)
>>> retrieve = driver.find_element_by_id("retrieve").click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="retrieve"]

元素似乎有两个不同的ID,我都尝试了这两个ID,但均未成功(即“ retrieve”和“ aView”)。

我也曾尝试通过部分链接文本('Retrieve')和xpath(// * [@ id =“ aView”])搜索,但没有成功。

任何建议都值得赞赏。

page behind login page

element circled in red, arrow pointing to html

1 个答案:

答案 0 :(得分:0)

您为按钮使用了错误的ID。它具有“ btnLogin” ID。使用该ID查找您的登录按钮。

附加屏幕截图以供参考。enter image description here