【问题标题】:How do I get my clicks to work on the website https://www.costcobusinessdelivery.com using Selenium through Python如何通过 Python 使用 Selenium 在网站 https://www.costcobusinessdelivery.com 上获得点击次数
【发布时间】:2020-01-26 19:49:21
【问题描述】:

当我在 Selenium 中运行此代码时,它只是坐在那里永远加载网页。有没有人能让这个代码工作并将我带到登录表单?

from selenium import webdriver;
from selenium.webdriver.support.ui import Select;
from selenium.webdriver.common.keys import Keys;
from selenium.webdriver.common.by import By;
from selenium.webdriver.chrome.options import Options;
from selenium.webdriver.support.ui import WebDriverWait;
from selenium.webdriver.support import expected_conditions as EC;
import time;



browser = webdriver.Chrome()
browser.get('https://www.costcobusinessdelivery.com')

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header_sign_in"))).click()

在网页保持无响应大约 5 分钟后,我收到此错误:

TimeoutException                          Traceback (most recent call last)
<ipython-input-6-47279e187da7> in <module>
     11 
     12 browser = webdriver.Chrome()
---> 13 browser.get('https://www.costcobusinessdelivery.com')
     14 
     15 WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header_sign_in"))).click()

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in get(self, url)
    331         Loads a web page in the current browser session.
    332         """
--> 333         self.execute(Command.GET, {'url': url})
    334 
    335     @property

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

TimeoutException: Message: timeout
  (Session info: chrome=77.0.3865.75)

【问题讨论】:

  • 运行此代码时是否启动了浏览器?
  • 是的,浏览器启动,网站正常打开。但是,此后它似乎不断加载/冻结。
  • @jest3rz 你的下一步是什么?
  • @debanjanb 发送.keys 到登录表单以登录网站。我无法访问登录表单,因为网页没有响应并且正在加载。
  • @jest3rz - 我还复制的页面在自动化脚本打开时没有响应。在手动上工作正常。我也在等着看答案。

标签: python selenium xpath css-selectors partiallinktext


【解决方案1】:

click() 在网站https://www.costcobusinessdelivery.com/ 内以登录/注册 为文本的链接,您必须为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies:

  • 使用PARTIAL_LINK_TEXT

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sign In / Register"))).click()
    
  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header_sign_in"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='header_sign_in']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

【讨论】:

  • 新代码不起作用,selenium 位于 www.costcobusinessdelivery.com 网页上并且永远不会加载。大约 5 分钟后,我收到此错误:TimeoutException: Message: timeout (Session info: chrome=77.0.3865.75)
猜你喜欢
  • 1970-01-01
  • 2021-01-06
  • 2021-04-09
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2019-10-28
  • 1970-01-01
相关资源
最近更新 更多