问题描述
在我文本的这一部分中,我将循环选中多个页面中的所有复选框(成功)。当下一个按钮不再启用时(因此没有更多的页面要翻,请尝试命令),我想单击应用按钮。有两种不同的应用按钮,一种可见的和不可见的。我的文本的编写方式,我收到一个元素不可交互异常,因此我的超时异常没有被看到。这是出乎意料的,因为我尝试点击的按钮清晰可见且可点击(手动)。
原始网站:https://icem.data-archive.ac.uk/#step2
有谁知道是什么导致此元素不可交互?我附上了两个应用按钮的 HTML 照片:https://i.stack.imgur.com/xBl9X.png
注意:在异常命令上引入 webdriverwait 只会引发另一个超时异常。
county_pop < 1000000:
while True:
checklist = webD.find_elements_by_xpath('//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')
for elem in checklist:
elem.click()
try:
webdriverwait(webD,10).until(EC.element_to_be_clickable((By.XPATH,'//*[not(contains(@class,"disabled"))]/a[.="Next"]'))).click()
except TimeoutException:
webD.find_element_by_xpath('//div[@class="modal-content"]//div[@class="col-sm-3 text-left visible-xs"]/button').click()
脚本直到那时:
import selenium
import time
from selenium import webdriver as wd
from selenium.webdriver.support.ui import webdriverwait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException,NoSuchElementException,NoSuchWindowException
from selenium.webdriver.common.action_chains import ActionChains
chrome_path = r'C:\webdrivers\chromedriver.exe'
webD=wd.Chrome(executable_path=chrome_path)
webD.get('https://beta.ukdataservice.ac.uk/myaccount')
webdriverwait(webD,20).until(EC.element_to_be_clickable(
(By.XPATH,'//*[@id="myaccount-login"]/div/div/div/div/div/div[2]/div/div/div/form/div/p/button'))).click()
webdriverwait(webD,'//*[@id="username"]'))).send_keys('ukd1217078879')
webdriverwait(webD,'//*[@id="password"]'))).send_keys('Census4Me!')
webD.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/form/div[6]/button').click()
webD.get('https://icem.data-archive.ac.uk/#step1')
#YEARS LOOP BEGINS
for year in [1851,1861,1881,1891,1901,1911]:
webD.find_element_by_xpath("//b[@class='ng-binding' and text()='{}']/preceding-sibling::input[@type='radio']".format(str(year))).click()
# Continuing to England & Step 2,opening Counties Variable,More Variables
webdriverwait(webD,20).until(EC.element_to_be_clickable((By.XPATH,'//b[@id = "country_england"]/preceding-sibling::input'))).click()
webD.find_element_by_xpath('//html/body/div/section/section[1]/article[2]/div/div/button').click()
webdriverwait(webD,'//*[@id="county_category"]/button[1]'))).click()
webdriverwait(webD,'//*[@id="county_category"]/div[2]/div/div[2]/button'))).click()
# COUNTIES LOOP BEGINS
conto = 1
count = conto
while count > 10: #Getting to the correct county page
webD_find_element_by_link_text('Next').click()
count = count - 10
for i in range(count,11):
#Clicking correct county
webdriverwait(webD,'/html/body/div[3]/div/div/div[3]/div[{}]/label/input'.format(count)))).click()
#Adding 1 to absolute count,conto
conto = conto + 1
#Store variables
county_text = webD.find_element_by_xpath('/html/body/div[3]/div/div/div[3]/div[{}]/label'.format(count)).text
county_pop = int(county_text.split("(")[-1].replace(")","").replace(",",""))
print(county_pop)
#Apply County Selection,open HISCO & More Variables
webdriverwait(webD,"//button[contains(.,'Apply')]"))).click()
webdriverwait(webD,20).until(EC.presence_of_element_located((By.XPATH,'//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))
webdriverwait(webD,20).until(EC.invisibility_of_element_located((By.XPATH,'//b[text()[contains(.,"HISCO classified occupation")]]/..'))).click()
webdriverwait(webD,'//*[@id="workhistoryunit_category"]/div[2]/div/div[2]/button'))).click()
#HISCO OCCUPATION SELECTION LOOP
if county_pop < 1000000:
while True:
checklist = webD.find_elements_by_xpath('//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')
for elem in checklist:
elem.click()
try:
webdriverwait(webD,"disabled"))]/a[.="Next"]'))).click()
except TimeoutException:
webD.find_element_by_xpath('//div[@class="modal-content"]//div[@class="col-sm-3 text-left visible-xs"]/button').click()
解决方法
temp = True
if county_pop < 1000000:
while temp:
checklist = webD.find_elements_by_xpath(
'//*[@class="modal-content"]//input[@class="ng-pristine ng-valid"]')
for elem in checklist:
elem.click()
try:
WebDriverWait(webD,10).until(EC.element_to_be_clickable(
(By.XPATH,'//*[not(contains(@class,"disabled"))]/a[.="Next"]'))).click()
except:
WebDriverWait(webD,'//button[contains(text(),"Apply")]'))).click()
temp = False
在 while 循环中,您有 True 这将使其无限运行,将其更改为可变的 Temp 并在下一个按钮不可点击时将其设置为 false。
同样使用 webdriver 等待并点击第一个按钮