[英]Why does this selenium not click “next page” until end?
我正在為網站Upwork編寫抓取代碼,並且需要單擊每個頁面以獲取工作清單。 這是我的python代碼,我使用selenium進行了Web爬網。
from bs4 import BeautifulSoup
import requests
from os.path import basename
from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("./chromedriver")
driver.get("https://www.upwork.com/o/jobs/browse/c/design-creative/")
link = driver.find_element_by_link_text("Next")
while EC.elementToBeClickable(By.linkText("Next")):
wait.until(EC.element_to_be_clickable((By.linkText, "Next")))
link.click()
有幾個問題:
EC
沒有屬性elementToBeClickable
。 在Python
您應該使用element_to_be_clickable
link
僅在第一頁上定義,因此在第二頁上使用它應為您提供StaleElementReferenceException
您的代碼中沒有定義wait
變量。 我想你的意思是
wait = WebDriverWait(driver, 10)
By
沒有屬性linkText
。 改用LINK_TEXT
嘗試使用以下代碼來獲取所需的行為
from selenium.common.exceptions import TimeoutException
while True:
try:
wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, Next"))).click()
except TimeoutException:
break
這應該允許您在可用時單擊“ Next
按鈕
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.