在Selenium中使用Python单击按钮时,如何将使用React呈现的页面重定向到其他页面?

在Selenium中使用Python单击按钮时,如何将使用React呈现的页面重定向到其他页面?,python,reactjs,selenium,redirect,web-scraping,Python,Reactjs,Selenium,Redirect,Web Scraping,我目前正在尝试刮取一个页面的内容,比如说它被称为https://example.com/home。当我在该页面的浏览器上导航时,我被重定向到此页面https://example.com/home/quiz我需要单击某个按钮才能退出该页面并重新激活到https://example.com/home。我试图在python代码中重现这一点,如下所示: try: url = "https://example.com/home" driver.get(url)

我目前正在尝试刮取一个页面的内容,比如说它被称为
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)

这是
https://example.com/home/quiz
使用React.js呈现的页面:


有人知道为什么我不能重定向回
https://example.com/home
单击按钮后?我不知道发生了什么事,任何帮助都将不胜感激!提前感谢

最有可能的是,您忘记将
click()
方法添加到按钮中。否则,它可能与您的选择器有关

 button = driver.find_element_by_xpath("//*[@id='course-placeholder']/div/div[2]/div[3]/div[2]/a")
 button.click()
如果上述解决方案无法解决您的问题,请尝试以下方法:

button = driver.find_element_by_css_selector("a.overlay-close")
button.click()

您没有单击按钮,只是获取按钮元素。无法打印
按钮。单击()
。。。没什么。打印
按钮
将打印该特定元素的某些ID,并且无法读取。