[英]How to click the button and get the source of new page with python-selenium?
[英]how to get new page's source code in selenium with python
假设有一个页面,其中有一个“下一页”按钮。
使用webdriver.Chrome(chrome_driver_path)
初始化browser
。
我使用page_source
属性来获取原始页面的源代码并找到下一页的按钮。 然后我点击它,浏览器将转到 Chrome 中同一选项卡中的下一页。
browser.get(origin_page_url)
browser.find_element_by_xpath(btn_xpath).click()
time.sleep(5)
在上面的代码之后,我再次调用page_source
,但我得到的与原始页面的源代码相同。
如何获取新打开页面的源代码?
在获取页面源之前,您需要explicitly wait
新页面加载。 这通常取决于您正在使用的网页。 例如,您可以等到特定元素变得可见:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))
element.click();
之后调用page_source
进口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
我还没有测试过这个,但是,肯定这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.