目前硒的问题相当严重
我正在尝试获取页面上的所有链接,单击每个链接,从页面获取数据并返回。即使在使用StaleElementReference异常处理程序时,它也会完全中断循环,尽管建议使用driver.back()
代码如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from datetime import datetime
from pymongo import MongoClient
from selenium.common.exceptions import StaleElementReferenceException
options = Options()
options.page_load_strategy = 'none'
# options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
url = "https://www.depop.com/purevintage_clothing/"
# driver = webdriver.Chrome()
driver.get(url)
for link in links:
linkClass = link.get_attribute("class")
try:
if str(linkClass[:19]) == "styles__ProductCard":
action = ActionChains(driver)
action.move_to_element(link)
action.click().perform()
product = doSomethingFunction()
if product != None:
insertIntoDatabase(product)
driver.back()
except StaleElementReferenceException as e:
print(e)
driver.back()
我知道缩进在这里有点不可靠,我手工写了出来,因为其余的处理代码,如insertIntoDatabase,我不确定是否与这里相关(如果您需要所有代码,请让我知道)
每当我这样做的时候,我都会在一个循环中出现错误异常,尽管有driver.back()
,我确信答案就在眼前,我有点看不清,但是这里非常感谢您的帮助
每次您返回主页时,您都需要获取链接,因为自从您更改页面后,它们不再出现在DOM中;因此,您应该按照以下步骤操作:
相关问题 更多 >
编程相关推荐