Selenium - 消息:没有这样的元素:无法找到元素

发布于 2025-01-15 05:32:01 字数 1404 浏览 1 评论 0 原文

代码试验:

img_source = driver.find_element_by_css_selector('#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-area > div.reader-pages-container > div:nth-child(4) > img.f.e').get_attribute('src')

thumbs = driver.find_element_by_css_selector('div.thumbnail-storage')
driver.execute_script('arguments[0].scrollBy(arguments[0].scrollWidth, 0)', thumbs)
sleep(1)
pages = driver.find_elements_by_css_selector('#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-scrubber-container > div > div:nth-child(1) > h1 > div.pages-counter > span:nth-child(3)')

图像 url 的 Html:

<img crossorigin="anonymous" src="blob:https://1.mysite/7e50ba18-3f0f-4b8d-acff-5684d3d551bc" class="f e" style="width: 2000px; height: 2697px;">

运行机器人时告诉我错误:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-area > div.reader-pages-container > div:nth-child(4) > img.f.o"}

Code trials:

img_source = driver.find_element_by_css_selector('#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-area > div.reader-pages-container > div:nth-child(4) > img.f.e').get_attribute('src')

thumbs = driver.find_element_by_css_selector('div.thumbnail-storage')
driver.execute_script('arguments[0].scrollBy(arguments[0].scrollWidth, 0)', thumbs)
sleep(1)
pages = driver.find_elements_by_css_selector('#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-scrubber-container > div > div:nth-child(1) > h1 > div.pages-counter > span:nth-child(3)')

Html for image url:

<img crossorigin="anonymous" src="blob:https://1.mysite/7e50ba18-3f0f-4b8d-acff-5684d3d551bc" class="f e" style="width: 2000px; height: 2697px;">

When run bot tell me error:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#root > div > div > div > div.MainFrameContainer > div > div > div > div > div > div > div.reader-area > div.reader-pages-container > div:nth-child(4) > img.f.o"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2025-01-22 05:32:01

根据 HTML:

<img crossorigin="anonymous" src="blob:https://1.mysite/7e50ba18-3f0f-4b8d-acff-5684d3d551bc" class="f e" style="width: 2000px; height: 2697px;">

要提取 src 属性的值,您可以使用以下任一 定位器策略

  • 使用css_selector

    print(driver.find_element_by_css_selector("img[crossorigin='anonymous'][src^='blob']").get_attribute("src"))
    
  • 使用xpath

    print(driver.find_element_by_xpath("//img[@crossorigin='anonymous' andstarts-with(@src, 'blob')']").get_attribute("src"))
    

理想情况下,您需要为 WebDriverWait /stackoverflow.com/a/50474905/7429447">visibility_of_element_ located() 并且您可以使用以下任一方法定位器策略

  • 使用CSS_SELECTOR

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.CSS_SELECTOR, "img[crossorigin='anonymous'][src^='blob']"))).get_attribute( “源”))
    
  • 使用XPATH

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_ located((By.XPATH, "//img[@crossorigin='anonymous' andstarts-with(@src, 'blob')') ]"))).get_attribute("src"))
    
  • 注意:您必须添加以下导入:

    从 selenium.webdriver.support.ui 导入 WebDriverWait
    从 selenium.webdriver.common.by 导入
    从 selenium.webdriver.support 导入预期条件作为 EC
    

As per the HTML:

<img crossorigin="anonymous" src="blob:https://1.mysite/7e50ba18-3f0f-4b8d-acff-5684d3d551bc" class="f e" style="width: 2000px; height: 2697px;">

To extract the value of the src attribute you can use either of the following locator strategies:

  • Using css_selector:

    print(driver.find_element_by_css_selector("img[crossorigin='anonymous'][src^='blob']").get_attribute("src"))
    
  • Using xpath:

    print(driver.find_element_by_xpath("//img[@crossorigin='anonymous' and starts-with(@src, 'blob')']").get_attribute("src"))
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[crossorigin='anonymous'][src^='blob']"))).get_attribute("src"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[@crossorigin='anonymous' and starts-with(@src, 'blob')']"))).get_attribute("src"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文