【问题标题】:Selenium: selenium.common.exceptions.TimeoutException: Message: error硒:selenium.common.exceptions.TimeoutException:消息:错误
【发布时间】:2021-09-11 07:34:29
【问题描述】:

我试图运行此脚本以使用 selenium 自动化并单击网页中的产品列表。但是这个“raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: error" 每次都发生。我在这里做错了什么?期待您的指导。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

chrome_options = Options()
chrome_options.add_argument('__headless')

chrome_path = which('chromedriver')

driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')

product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[@class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()


time.sleep(10)

driver.close()

输出

    PS G:\Python_Practice\scrapy_practice\test> [21628:13792:0911/125833.339:ERROR:gpu_init.cc(441)] Passthrough is not supported, GL is disabled
                                          > & C:/Users/raisu/anaconda3/envs/Scrapy_Workspace2/python.exe g:/Python_Practice/scrapy_practice/test/test.py

DevTools listening on ws://127.0.0.1:56456/devtools/browser/1d6d20ce-ecb9-44f7-be6e-1dbe1373526a
Traceback (most recent call last):
  File "g:/Python_Practice/scrapy_practice/test/test.py", line 18, in <module>
    product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[@class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()
  File "C:\Users\raisu\anaconda3\envs\Scrapy_Workspace2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

【问题讨论】:

  • 看起来他们将 Selenium 检测为可疑活动。我在 Selenium 中尝试了几乎所有可能的方式,但无法使其工作。我认为你必须诱导代理,这样它就不会被检测到。

标签: selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

我没有使用 xpath,而是使用了 CSS_SELECTOR,并且我还更改了元素可见的等待条件

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

chrome_options = Options()
chrome_options.add_argument('__headless')

chrome_path = which('chromedriver')

driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')

#time.sleep(5)   use this only if the wait is not working
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'article > a')))

driver.find_element(By.CSS_SELECTOR,'article > a').click()



driver.close()

【讨论】:

  • PS G:\Python_Practice\scrapy_practice\test> & C:/Users/raisu/anaconda3/envs/Scrapy_Workspace2/python.exe g:/Python_Practice/scrapy_practice/test/test2.py DevTools 正在监听ws://127.0.0.1:52109/devtools/browser/e022ca0d-9fcd-44a1-95b8-166404088494 Traceback(最近一次调用最后):文件“g:/Python_Practice/scrapy_practice/test/test2.py”,第 21 行,在 wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'article > a'))) AttributeError: module 'selenium.webdriver.support.wait' has no attribute 'until' PS G:\Python_Practice\scrapy_practice \测试>
猜你喜欢
  • 2021-04-20
  • 2021-03-25
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2015-11-21
  • 2019-04-22
相关资源
最近更新 更多