python硒 - 鼻琴感应,尽管发现了该值

发布于 2025-02-08 08:11:32 字数 1924 浏览 4 评论 0原文

我在AWS上的Lambda功能中运行了Python Selenium脚本。

使用XPath,我尝试找到分页按钮移至下一页。

我使用以下代码:

button_next = driver.find_element_by_xpath('//a[@data-at="pagination-next"]')

代码有效,因为我能够打印它并将事件提取URL并打印出来:

url = button_next.get_attribute("href")

但是在日志中,我会收到以下错误,但是在我能够打印之后:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@data-at="pagination-next"]"}
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.14.255-276-224.499.amzn2.x86_64 x86_64)
: NoSuchElementException
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 69, in lambda_handler
    button_next = driver.find_element_by_xpath('//a[@data-at="pagination-next"]')
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 368, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 858, in find_element
    'value': value})['value']
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@data-at="pagination-next"]"}
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.14.255-276-224.499.amzn2.x86_64 x86_64)

什么是问题这里?尽管XPath以前刚刚起作用,但为什么我会遇到此错误。

I'm running a python selenium script in a lambda function on AWS.

With xpath I try to find the pagination button to move to the next page.

I use the following code:

button_next = driver.find_element_by_xpath('//a[@data-at="pagination-next"]')

The code works, because I'm able to print it and event extract the URL and also print it:

url = button_next.get_attribute("href")

But in the log I get the following error but after I was able to print it:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@data-at="pagination-next"]"}
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.14.255-276-224.499.amzn2.x86_64 x86_64)
: NoSuchElementException
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 69, in lambda_handler
    button_next = driver.find_element_by_xpath('//a[@data-at="pagination-next"]')
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 368, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 858, in find_element
    'value': value})['value']
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/opt/python/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@data-at="pagination-next"]"}
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.14.255-276-224.499.amzn2.x86_64 x86_64)

What is the issue here? Why do I get this error, although the xpath just worked before.

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

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

发布评论

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

评论(1

魔法少女 2025-02-15 08:11:32

所需的元素是动态元素,因此要单击 clickable 元素,您需要诱导 webdriverwait 对于 emeltion_to_be_clickable()以下 定位器策略

  • 使用 css_selector ::::

      webdriverwait(驱动程序,20).until(ec.element_to_be_be_clickable(((by.css_selector),“ a [data-at ='pagination-next']))。
     
  • 使用 xpath

      webdriverwait(驱动程序,20).until(ec.element_to_be_be_clickable(((by.xpath,“ // a [@data-at-at ='pagination-next']))。
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

另外,请确保:

The desired element is a dynamic element so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[data-at='pagination-next']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-at='pagination-next']"))).click()
    
  • 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
    

Additionally, ensure that:

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