代码之家  ›  专栏  ›  技术社区  ›  Pygirl

当我执行驱动程序时,Selenium会将我重定向回起始页。获取(url)

  •  0
  • Pygirl  · 技术社区  · 7 年前

    我正在删除此网页: http://www.cpcb.gov.in/CAAQM/frmReportdisplay.aspx 单击“提交”按钮后,页面内容会发生更改,而url保持不变。我想访问这些内容,但在打印(driver.current\u url)时无法访问,因为我看到了起始页的内容。 如何访问这些内容?

    提交后: 页面内容就是这个。

    enter image description here

    from bs4 import BeautifulSoup
    import sys
    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    import time 
    chromedriver_loc = '/home/ninjakx/Desktop/mywork/chromedriver-Linux64' 
    # enter path of chromedriver
    driver = webdriver.Chrome(executable_path=chromedriver_loc)
    url ="http://www.cpcb.gov.in/CAAQM/frmUserAvgReportCriteria.aspx"
    driver.get(url)
    
    select = Select(driver.find_element_by_id('ddlState'))
    # select by visible text
    select.select_by_visible_text('Delhi')
    time.sleep(10)
    select = Select(driver.find_element_by_id('ddlCity'))
    select.select_by_visible_text('Delhi')
    time.sleep(5)
    select = Select(driver.find_element_by_id('ddlStation'))
    select.select_by_visible_text('Dwarka')
    time.sleep(10)
    
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='874']")
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='1366']")
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='1377']")
    
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='864']")
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='824']")
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    your_choice=driver.find_element_by_xpath("//select[@id='lstBoxChannelLeft']/option[@value='502']")
    your_choice.click()
    driver.find_element_by_xpath('//*[@id="btnAdd"]').click()
    time.sleep(13)
    
    datefield = driver.find_element_by_xpath('//*[@id="txtDateFrom"]')
    datefield.click()
    datefield.clear()
    datefield.send_keys("01/01/2017")
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()
    time.sleep(5)
    cur_url = driver.current_url
    driver.get(url)
    time.sleep(20)
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Avaricious_vulture    7 年前

    这不仅仅是一个打字错误吗?

    您键入了:

    driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()
    time.sleep(5)
    cur_url = driver.current_url
    driver.get(url)
    

    我认为应该是:

    driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()
    time.sleep(5)
    cur_url = driver.current_url
    driver.get(cur_url) #changed here
    
        2
  •  0
  •   Pygirl    7 年前

    我不必再次访问链接。只需继续编写脚本即可获取数据。