【发布时间】:2019-08-29 13:52:38
【问题描述】:
我目前正在尝试抓取页面的内容,比如说它叫做 https://example.com/home 。当我在该页面上的浏览器上导航时,我被重定向到此页面https://example.com/home/quiz,我需要单击一个按钮才能退出该页面并重新导航到https://example.com/home。我试图在我的 python 代码上重现它,如下所示:
try:
url = "https://example.com/home"
driver.get(url)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
time.sleep(10)
#this page prints https://example.com/home/quiz
print(driver.current_url)
#button which redirects to https://example.com/home
button = driver.find_element_by_xpath("//*[@id='course-placeholder']/div/div[2]/div[3]/div[2]/a")
print(button)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
time.sleep(10)
#get current page
#this page prints also https://example.com/home/quiz
print(driver.current_url)
except Exception as e:
print(e)
这是使用 React.js 呈现的 https://example.com/home/quiz 页面的结构:
有谁知道为什么我在点击按钮后不能被重定向回https://example.com/home?我不知道发生了什么,任何帮助将不胜感激!提前致谢
【问题讨论】:
-
你没有点击按钮,只是获取按钮元素。
标签: python reactjs selenium redirect web-scraping