首页 > 解决方案 > 点击使用 Selenium 的一系列问题——无法通过第一个问题

问题描述

我正在尝试使用带有 Python 的 Selenium 单击测试网站上的一系列问题。目前,选择哪个答案并不重要——我只想能够从一个问题转到下一个问题。这是我到目前为止所拥有的:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Firefox(executable_path="../../geckodriver.exe")
driver.get('https://www.varsitytutors.com/practice-tests')
wait = WebDriverWait(driver, 10)
# click subject
subject=driver.find_element_by_xpath('/html/body/div[3]/div[9]/div/div[2]/div[1]/div[1]')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[9]/div/div[2]/div[1]/div[1]')))
subject.click()
# select specialty
specialty=driver.find_element_by_xpath('/html/body/div[3]/div[9]/div/div[2]/div[2]/div[1]/div[2]/a[4]')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[9]/div/div[2]/div[2]/div[1]/div[2]/a[4]')))
specialty.click()
# select test
taketest=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[8]/div[3]/div[1]/div[1]/a[1]')))
driver.execute_script("arguments[0].click();", taketest)
wait.until(EC.url_contains('diagnostic'))
driver.get(driver.current_url.replace('http', 'https'))

# click away popup
button=WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks')]")))
button.location_once_scrolled_into_view
button.click()

# select first choice
choice=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[7]/div[1]/div[3]/div[1]/form/input[1]')))
driver.execute_script("arguments[0].click();", choice)

最后一个命令做了它应该做的事情。据我所知,下一个命令应该做同样的事情——点击下一个问题的第一个答案......

# select first choice again
choice=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[7]/div[1]/div[3]/div[1]/form/input[1]')))
driver.execute_script("arguments[0].click();", choice)

然而,这不起作用。连接丢失并出现 Firefox 错误页面。

我的目标是点击这个特定测试中的整个系列问题。谁能帮我弄清楚该怎么做?

标签: pythonseleniumxpathweb-scraping

解决方案


使用infinite while循环并提供Try..Except块来检查选项是否存在然后单击否则转到异常块并退出。尝试下面的代码。我没有测试所有问题但是循环工作正常。让我知道它是怎么回事。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver=webdriver.Firefox(executable_path="../../geckodriver.exe")
driver.get('https://www.varsitytutors.com/practice-tests')
wait = WebDriverWait(driver, 10)
# click subject
subject=wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@data-subject='ACT']/div[1]")))
subject.click()
# select specialty
specialty=wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@data-subject='ACT English']//div[@class='expandable']//a[contains(.,'Practice Tests')]")))
specialty.click()
# select test
taketest=wait.until(EC.element_to_be_clickable((By.XPATH,"//h3[text()='ACT English Diagnostic Test 1']/following::div[1]/a[1]")))
driver.execute_script("arguments[0].click();", taketest)

# click away popup
button=wait.until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'No Thanks')]")))
button.location_once_scrolled_into_view
button.click()

# select any choice
while True:
   try:
     choice=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"input.test_button")))
     driver.execute_script("arguments[0].click();", choice[3])
     time.sleep(3)
   except:
     break

推荐阅读