当我执行driver.get(url)时,Selenium会将我重定向回起始页面

时间:2018-01-29 20:28:48

标签: python-3.x selenium web-scraping selenium-chromedriver

我正在废弃此网页:http://www.cpcb.gov.in/CAAQM/frmReportdisplay.aspx 点击提交按钮后,页面内容会更改,而网址仍然相同。我想访问这些内容,但是当我打印(driver.current_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 个答案:

答案 0 :(得分:0)

这不只是打字错误吗?

你打字:

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

答案 1 :(得分:0)

我不必再次访问该链接。必须继续编写脚本来获取数据。