[英]Selenium WebDriver Click skips some checkboxes
This is a follow-up question to this: 这是对此的后续问题:
WebDriver element found, but click returns nothing 找到了WebDriver元素,但单击不返回任何内容
I am trying to scrape data from the URL in the code after making selections in the drop-down menu. 在下拉菜单中进行选择后,我试图从代码中的URL抓取数据。 I first click on Progress Monitoring and then Physical and Financial Project Summary. 我首先单击“进度监视”,然后单击“实物和财务项目摘要”。 Then I make the following selections: State, District, Block, Year, Batch, and Collaboration. 然后,我进行以下选择:州,地区,街区,年份,批次和协作。 I would also like to check the Road Wise button and then click on the view button. 我还想检查Road Wise按钮,然后单击查看按钮。 After the table loads, I would like to click on the save button and download the excel file. 加载表格后,我想单击“保存”按钮并下载excel文件。 In the code below I also loop through different selections under "State" item. 在下面的代码中,我还将循环浏览“状态”项下的不同选择。 Here is my code: 这是我的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import os
chromedriver = r"C:\Users\yuppal\chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(chromedriver)
browser.implicitly_wait(10)
browser.get("http://omms.nic.in")
browser.maximize_window()
#Click on the item Progress Monitoring
progElem = browser.find_element_by_link_text("Progress Monitoring").click()
#Click on the item Physical and Financial Project Sumamry
summElem = browser.find_element_by_link_text("Physical and Financial Project Summary").click()
#Find the element for state and create a list of different selection options
stateElem = browser.find_element_by_xpath("//select[@name='StateCode']")
state_options = stateElem.find_elements_by_tag_name("option")
#delete the first option in the list
del state_options[0]
def select_option(xpath, text):
'''
This function will select the remaining dropd-down menu items.
'''
elem = browser.find_element_by_xpath(xpath)
Select(elem).select_by_visible_text(text)
#run the loop for each option in the list of states
for option in state_options:
select_state = Select(stateElem).select_by_value(option.get_attribute("value"))
# Select the district.
select_option("//select[@name='DistrictCode']","All Districts")
# Select the block.
select_option("//select[@name='BlockCode']","All Blocks")
# Select the year.
select_option("//select[@name='Year']","All Years")
# Select the batch.
select_option("//select[@name='Batch']","All Batches")
# Select the funding agency.
select_option("//select[@name='FundingAgency']","Regular PMGSY")
# Check the road wise box.
time.sleep(10)
checkElem = WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, "//input[@title='Road Wise']")))
browser.execute_script("arguments[0].click();", checkElem)
# Click on the view button.
time.sleep(10)
browser.find_element_by_xpath("//input[@type='button']").click()
# Switch to a new frame.
time.sleep(10)
frame = browser.find_element_by_xpath("//div[@id='loadReport']/iframe")
browser.switch_to.default_content()
#browser.switch_to.frame(frame)
WebDriverWait(browser, 120).until(EC.frame_to_be_available_and_switch_to_it(frame))
#browser.switch_to.frame(browser.find_element_by_xpath("//*[@id='loadReport']/iframe"))
# click on the save button
time.sleep(10)
WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Export drop down menu']"))).click()
# Within the save button, Click on the "Excel" option.
time.sleep(10)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div/a[@title='Excel']"))).click()
# Switch back to the main content.
time.sleep(20)
browser.switch_to.default_content()
My issue is the "Road Wise" checkbox gets clicked only for some states. 我的问题是仅在某些州单击“明智的道路”复选框。 Thus the loop proceeds without clicking the checkbox for some states. 因此,循环会继续进行,而无需单击某些状态的复选框。 I checked the HTML code and it is the same for all checkboxes. 我检查了HTML代码,所有复选框都相同。
I thought the problem might be that the "View" button gets clicked before the road wise button is clickable. 我认为问题可能出在“道路”按钮可单击之前,先单击了“查看”按钮。 So I put some waiting period before both road wise and view buttons. 因此,我在道路明智和查看按钮之前放置了一些等待时间。 But that doesn't seem to help. 但这似乎无济于事。 So I can't really understand why the checkbox button isn't clicked for some iterations in the loop. 因此,我无法真正理解为什么循环中的某些迭代未单击复选框按钮。
Before clicking on the checkbox, check that is already selected or not: 在单击复选框之前,请检查是否已被选中:
# Check the road wise box.
time.sleep(10)
checkElem = WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, "//input[@title='Road Wise']")))
if checkElem.is_selected() != True:
browser.execute_script("arguments[0].click();", checkElem)
PS: In your case, the click will be only in the first iteration of the loop. PS:在您的情况下,点击只会在循环的第一次迭代中进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.