使用硒时无法定位元素

问题描述 投票:0回答:2

我正在尝试使用 Chrome 和 Selenium 登录网站,但是无法定位元素。我试过:

  1. 在其他网站上使用相同的代码,它有效。
  2. 找到页面中的所有元素,但没有找到。
    driver.find_elements_by_xpath('//*[@id]')
  • python 版本 = 3.8.1
  • 硒版本 = 3.141.0
  • 没有 iframe 影响

[在此处输入图片描述][1] [在此处输入图片描述][2]

    settings = Settings()
    opts = ChromeOptions()
    # 使用headless无界面浏览器模式
    # opts.add_argument('--headless')
    # opts.add_argument('--disable-gpu')
    opts.add_experimental_option("excludeSwitches", settings.selenium_opts_excludeSwitches)
    opts.add_experimental_option("prefs", settings.selenium_opts_prefs)
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=opts)

    opts = ChromeOptions()
    # opts.headless = False

    driver.get('http://itdms01/otcs/llisapi.dll?func=LL.getlogin&NextURL=%2Fotcs%2Fllisapi%2Edll%3FRedirect%3D1')

    # time.sleep(1)

    input = driver.find_element_by_id('Username')
    input.send_keys(settings.login_name)

执行日志:

[WDM] - Current google-chrome version is 113.0.5672
[WDM] - Get LATEST driver version for 113.0.5672
 
[WDM] - Get LATEST driver version for 113.0.5672
[WDM] - Trying to download new driver from http://chromedriver.storage.googleapis.com/113.0.5672.63/chromedriver_win32.zip
[WDM] - Driver has been saved in cache [C:\Users\deche\.wdm\drivers\chromedriver\win32\113.0.5672.63]
[WDM] - Current google-chrome version is 113.0.5672
[WDM] - Get LATEST driver version for 113.0.5672
 
[WDM] - Driver [C:\Users\deche\.wdm\drivers\chromedriver\win32\113.0.5672.63\chromedriver.exe] found in cache
Traceback (most recent call last):
  File "D:/decher_PC_google_drive/Documents/Git/WebCrawler/dmsCrawler/spider/spider_functions_v1.0.py", line 312, in <module>
    input = driver.find_element_by_id('Username')
  File "C:\Users\deche\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\deche\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\deche\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\deche\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="Username"]"}
  (Session info: chrome=113.0.5672.93)
python selenium-webdriver web-crawler
2个回答
0
投票

要获得更详细的答案,我需要查看您尝试在浏览器开发工具中抓取的元素。

但是对于我所看到的:

driver.find_element_by_id('')

有一直寻找CSS选择器的问题

{“方法”:“CSS选择器”..

我建议使用:

driver.find_elements_by_xpath('//*/*[@Id='Username']')

对于输入和按钮等标准元素,xpath 是可行的方法。


0
投票

谢谢回复。 我认为元素可以通过 selenium 和我的代码找到。

然而,当我通过 type() 函数打印出来时,元素类型是 。因此 selenuim 认为元素是对象而不是 html 元素。

本站位于公司内部网。我尝试另存为本地文件,问题可以重现。本地文件可以通过以下链接下载:https://drive.google.com/file/d/1fcfiwRCA1r1YyOwsZh3Sf6jxzxZRdCHY/view?usp=share_link

下面是我的 Python 代码:

from selenium import webdriver

try:
    browser = webdriver.Chrome('plug-in\chromedriver.exe')
    # browser.get('http://itdms01/otcs/llisapi.dll?func=LL.getlogin&NextURL=%2Fotcs%2Fllisapi%2Edll%3FRedirect%3D1')
    browser.get('file:///C:/Users/deche/Downloads/Content%20Server%20Log-in.html')
    input = browser.find_elements_by_xpath('//*[@id="Username"]')
    for item in input:
        print(type(item))
finally:
    print('11')
© www.soinside.com 2019 - 2024. All rights reserved.