使用Selenium单击一系列问题-无法超越第一个

时间:2019-10-14 11:58:54

标签: python selenium xpath web-scraping

我正在尝试使用带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错误页面。

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

1 个答案:

答案 0 :(得分:1)

使用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