Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium WebDriver单击会跳过一些复选框_Python_Selenium_Checkbox_Drop Down Menu - Fatal编程技术网

Python Selenium WebDriver单击会跳过一些复选框

Python Selenium WebDriver单击会跳过一些复选框,python,selenium,checkbox,drop-down-menu,Python,Selenium,Checkbox,Drop Down Menu,这是一个后续问题: 在下拉菜单中进行选择后,我试图从代码中的URL中刮取数据。我首先单击进度监控,然后单击物理和财务项目摘要。然后我做出以下选择:州、地区、街区、年份、批次和协作。我还想检查Road Wise按钮,然后单击view按钮。加载表后,我想单击save按钮并下载excel文件。在下面的代码中,我还循环了“状态”项下的不同选择。这是我的密码: from selenium import webdriver from selenium.webdriver.support.ui import

这是一个后续问题:

在下拉菜单中进行选择后,我试图从代码中的URL中刮取数据。我首先单击进度监控,然后单击物理和财务项目摘要。然后我做出以下选择:州、地区、街区、年份、批次和协作。我还想检查Road Wise按钮,然后单击view按钮。加载表后,我想单击save按钮并下载excel文件。在下面的代码中,我还循环了“状态”项下的不同选择。这是我的密码:

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()
我的问题是“道路智能”复选框只在某些州被点击。因此,循环继续进行,而不单击某些状态的复选框。我检查了HTML代码,所有复选框都是一样的


我想问题可能是“查看”按钮在道路智能按钮可点击之前就被点击了。所以我在“道路”和“视图”按钮前都设置了一些等待时间。但这似乎没有帮助。因此,我无法真正理解为什么循环中的某些迭代没有单击复选框按钮。

在单击复选框之前,请检查是否已选中复选框:

# 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:在您的情况下,单击将仅在循环的第一次迭代中进行。

您可以发布步骤吗?你该怎么办?谢谢。这听起来对你的情况非常具体,我很难知道如果不在我面前的站点上通过调试器,我将如何解决这个问题。这可能与网站本身的运行方式有关,我确信很难在这里详细说明。@RatmirAsanov我编辑了代码。我不确定它是否能给你更多关于我想做什么的信息。但希望它能有所帮助。谢谢@弗里斯特先生,我相信这不是该网站特有的。但是,是的,我不确定。@Yogesh在不了解有关该站点的更多详细信息的情况下,我的最佳猜测是,您的脚本单击了复选框,然后完成了一些脚本或ajax操作,刷新复选框以取消勾选,但此时您的脚本已经继续。您提到,您已经尝试将等待放在道路智能单击前面,但这应该可以绕过它。当您使用调试器时,复选框是否仍然会丢失?Ratmir,这很好。再次感谢你的帮助!