简体   繁体   English

为什么我的 for 循环在我的列表中途失败? Selenium webdriver,driver.find_element_by_xpath

[英]Why is my for loop failing halfway through my list? Selenium webdriver, driver.find_element_by_xpath

I have a list of 10 linkedin urls that I am running.我有一个我正在运行的 10 个linkedin url 的列表。 For 8 of the urls, the code works perfectly.对于其中的 8 个网址,代码运行良好。 But it consistently fails during the 9th iteration.但它在第 9 次迭代期间始终失败。

This is my traceback:这是我的回溯:

Traceback (most recent call last):
  File "/Users/yu/PycharmProjects/linkedin/hello.py", line 97, in <module>
    images = driver.find_element_by_xpath('// img[ @ id = "ember51"]')
  File "/Users/yu/PycharmProjects/linkedin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/yu/PycharmProjects/linkedin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Users/yu/PycharmProjects/linkedin/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/yu/PycharmProjects/linkedin/venv/lib/python3.8/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":"xpath","selector":"// img[ @ id = "ember51"]"}
  (Session info: chrome=80.0.3987.122)

Relevant code:相关代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from parsel import Selector

ChromeOptions = webdriver.ChromeOptions()
ChromeOptions.add_argument('--disable-browser-side-navigation')
driver = webdriver.Chrome('/Users/yu/Downloads/chromedriver')

linkedin_urls=driver.find_elements_by_xpath('//*[@id="rso"]/div[1]/div/div')
linkedin_urls=[url.find_element_by_xpath('div/div/div[1]/a').get_attribute('href') for url in linkedin_urls]

for linkedin_url in (linkedin_urls):
    driver.get(linkedin_url)
    sel = Selector(text=driver.page_source)

images = driver.find_element_by_xpath('// img[ @ id = "ember51"]')
print(images.get_attribute('src'))

Why would the code stop working?为什么代码会停止工作? I checked the 9th url , and the xpath is the same for images我检查了第 9 个 urlimages的 xpath 是相同的

It's failing on它失败了

images = driver.find_element_by_xpath('// img[ @ id = "ember51"]')

The element with the id of "ember51" is a div, so change it to this: id为“ember51”的元素是一个div,所以改成这样:

images = driver.find_element_by_xpath('// div[ @ id = "ember51"]/img')

This way when you try to get the src attribute on the next line, it will work.这样,当您尝试获取下一行的 src 属性时,它将起作用。

暂无
暂无

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

相关问题 对于 driver.find_element_by_xpath 中的 XPATH 循环不起作用 - For loop not working for XPATH in driver.find_element_by_xpath 为什么在 Selenium 命令 driver.find_element_by_xpath 不起作用? - Why in Selenium the command driver.find_element_by_xpath doesn't work? 硒无法使用driver.find_element_by_xpath捕获 - Can't capture by driver.find_element_by_xpath used by selenium 如果找不到某个 driver.find_element_by_xpath,如何让我的代码跳过一行 - How do I make my code skip a line if a certain driver.find_element_by_xpath isnt found selenium 驱动程序,button=driver.find_element_by_xpath(“//input[@id='alpha']”) 消息:没有这样的元素:无法定位元素 - selenium driver, button=driver.find_element_by_xpath(“//input[@id='alpha']”) Message: no such element: Unable to locate element Python Selenium driver.find_element_by_xpath 在 iframe 中找不到元素 - Python Selenium driver.find_element_by_xpath can't find element within iframe 不是有效的 xpath 表达式 driver.find_element_by_xpath - not a valid xpath expression driver.find_element_by_xpath 问题 &quot;driver.find_element_by_xpath(&quot;//div[@class=&#39;example&#39;]&quot; - Problem " driver.find_element_by_xpath("//div[@class='example']" 使用 driver.find_element_by_xpath().clear() 清除输入字段 - Clear input field with driver.find_element_by_xpath().clear() Selenium和Webdriver遍历find_element_by_xpath()的元素列表 - Loop over list of elements for find_element_by_xpath() by Selenium and Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM