Python:Phantomjs 找不到 chrome webdriver 工作正常的元素

Posted

技术标签:

【中文标题】Python:Phantomjs 找不到 chrome webdriver 工作正常的元素【英文标题】:Python: Phantomjs cannot find element where chrome webdriver works fine 【发布时间】:2021-11-05 21:22:38 【问题描述】:

我正在尝试从网页下载 CSV。当我单击页面中的图像时,下载开始。当我尝试使用ChromeWebDriver 这样做时,一切正常,但我需要使用PhantomJS,并且由于某种原因它无法找到该元素。 查找它,我发现有时改变它找到元素的方式就足够了,不幸的是,文本、img 和访问该元素的类在页面中多次出现reapeated。 有人可以帮我吗? 这是我的编码:

from selenium import webdriver
import time
    
PATH='vvv' 
prefs = 'download.default_directory' : 'vvv'
driver = webdriver.PhantomJS(PATH,desired_capabilities=prefs)
driver.get('https://infostat-ivass.bancaditalia.it/RIGAInquiry-public/ng/#/area-download')
driver.implicitly_wait(15)
target = driver.find_element_by_xpath("/html/body/inquiry-root/div/inquiry-area-download/div[2]/inquiry-grid/ag-grid-angular/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[5]/inquiry-pdf-csv/div/img")
target.click()
time.sleep(3)
driver.close()

这是错误信息:

Traceback (most recent call last):
  File "ffff.py", line 18, in <module>
    target = driver.find_element_by_xpath("/html/body/inquiry-root/div/inquiry-area-download/div[2]/inquiry-grid/ag-grid-angular/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[5]/inquiry-pdf-csv/div/img")
  File "fff\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "ffff\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, 
  File "ffff\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "fffff\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: "errorMessage":"Unable to find element with xpath '/html/body/inquiry-root/div/inquiry-area-download/div[2]/inquiry-grid/ag-grid-angular/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[5]/inquiry-pdf-csv/div/img'","request":"headers":"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"244","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62209","User-Agent":"selenium/3.141.0 (python windows)","httpVersion":"1.1","method":"POST","post":"\"using\": \"xpath\", \"value\": \"/html/body/inquiry-root/div/inquiry-area-download/div[2]/inquiry-grid/ag-grid-angular/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[5]/inquiry-pdf-csv/div/img\", \"sessionId\": \"9dbc06c0-113f-11ec-a5dd-dfc6f1996a7b\"","url":"/element","urlParsed":"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":,"chunks":["element"],"urlOriginal":"/session/9dbc06c0-113f-11ec-a5dd-dfc6f1996a7b/element"
Screenshot: available via screen

【问题讨论】:

【参考方案1】:

不确定确切的问题是什么,但您的 xpath 看起来像 brittle

试试这个而不是

//div[contains(text(),'Lista eventi')]//following-sibling::div[4]//descendant::div

对于任何特定的Descrizione,您也必须在此处进行更改,contains(text(),'Lista eventi')

【讨论】:

以上是关于Python:Phantomjs 找不到 chrome webdriver 工作正常的元素的主要内容,如果未能解决你的问题,请参考以下文章

Phantomjs 2.0.1 在 /assets/test-loader.js 找不到变量定义

Ghostdriver 1.2.1 + PhantomJS 2.0 + 最新的 Selenium 在 Java 中找不到变量错误

[Python爬虫] 之十一:Selenium +phantomjs抓取活动树中会议活动

Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

Selenium + PhantomJS打开的网页与常规浏览器不同

[python模块]selenium基本使用