【问题标题】:Selenium redirects me back to the starting page when i do driver.get(url)当我执行 driver.get(url) 时,Selenium 将我重定向回起始页面
【发布时间】:2018-01-29 20:28:48
【问题描述】:

我正在废弃这个网页:http://www.cpcb.gov.in/CAAQM/frmReportdisplay.aspx 点击提交按钮后,页面内容发生变化,而 url 保持不变。我想访问这些内容,但当我执行 print(driver.current_url) 时无法访问,我看到了起始页的内容。 我如何访问这些内容?

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

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)

【问题讨论】:

  • Iframe 里面的内容正在被更改..点击提交按钮?
  • 是的,只有内容在变化。
  • ...我可以看到页面网址正在更改...您请求的网址与单击提交按钮后的网址不同...比较脚本中的网址和屏幕截图中的网址
  • 好吧,我在 4 天前解决了它,所以现在我没有任何问题 :)

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


【解决方案1】:

这不仅仅是打字错误吗?

你输入了:

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

【讨论】:

  • 提交后url没有变化
  • 当我做 print(driver.get(current_url)) 我看到的起始页内容不是那些。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多