如何在硒中获得auth后的响应页面?

时间:2016-08-08 04:39:25

标签: python selenium

我需要在表单提交后获取响应页面,但我有相同的页面,其中包含空表单字段。我正在尝试使用time.sleep(),但它没有帮助。

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

phantom_js_executable_path = './bin/phantomjs.exe'
user_agent = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.37 (KHTML, like Gecko) Chrome/54.0.2705.89 Safari/536.37"
)

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent

def main():
    driver = webdriver.PhantomJS(executable_path = phantom_js_executable_path) # or add to your PATH
    driver.get('http://any.syte.my/login')
    user_name = driver.find_element_by_id("username")
    user_name.send_keys("user")
    password = driver.find_element_by_id("password")
    password.send_keys("pass")
    driver.find_element_by_name('continue').click()#form button submit
    #time.sleep(10)
    driver.save_screenshot('screen.png') # save a screenshot to disk


if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:0)

您需要在提交用户名和密码后等待页面中的元素。

ff = webdriver.Firefox()
ff.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:
    ff.quit()

这是http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

答案 1 :(得分:0)

以下代码将在您提交后等待元素出现,如果找不到元素,则会失败。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located(By.ID, elementFromNewPage))
driver.save_screenshot('weee.png')