【问题标题】:Selenium code that works in python/spyder does not works in colab在 python/spyder 中工作的 Selenium 代码在 colab 中不起作用
【发布时间】:2021-07-01 14:44:01
【问题描述】:

我在 Python 和 Colab 中使用 selenium。 我有一些在 spyder 中工作的代码,提取元素但在协作中给我错误

NoSuchElementException: Message: no such element: Unable to locate element:

什么是可能的解释,是否有可能解决这个问题?

【问题讨论】:

  • 之前,尝试与元素交互放置一个 time.sleep(certain_time)。这将使元素有时间加载。虽然这不是最佳做法,但请看这里stackoverflow.com/questions/59130200/…
  • 但这给了我另一个错误:
  • 那个错误是什么?
  • WebDriverWait(driver, 4).until(EC.presence_of_element_located((By.XPATH, "//li[@class='style__CategoryItem-sc-8ncu0g-2 tZtCz']//span[text()='%s']" %(aaa[1])))).click() TimeoutException: Message:

标签: python selenium selenium-webdriver google-colaboratory


【解决方案1】:

尝试使用这种在元素被访问之前等待的方法。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome(executable_path='path')            
waitshort = WebDriverWait(driver,.5)
wait = WebDriverWait(driver, 20)
waitLonger = WebDriverWait(driver, 100)
visible = EC.visibility_of_element_located        
driver.get('website')
element = wait.until(visible((By.XPATH,'element_xpath'))).click()

【讨论】:

  • 还是同样的错误:TimeoutException: Message: 即使我尝试了所有的等待选项
猜你喜欢
  • 2020-09-13
  • 2017-09-08
  • 2021-11-11
  • 2015-09-12
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2019-04-27
相关资源
最近更新 更多