【问题标题】:Landing Page Not Loading - Selenium Web Form Automation登陆页面未加载 - Selenium Web 表单自动化
【发布时间】:2018-10-15 22:24:31
【问题描述】:

创建一个脚本,该脚本将登录到下面的站点并自动将值记录到 Web 表单中。问题是,一旦我登录,登录页面是空白的(即它加载标题但仅此而已)。我的代码如下:

profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
browser = webdriver.Firefox(profile)
browser.get('https://cmsdb.darkcosmos.org/experiments/run/new')
print('Connected to Server')
time.sleep(2) # Wait for page to load
login_button = browser.find_element_by_xpath('/html/body/div/div[5]/main/div/div[1]/div/div[3]/button')
login_button.click()
time.sleep(2) # Wait for pop-out to load
browser.find_element_by_xpath('//*[@id="username"]').send_keys(username)
browser.find_element_by_xpath('//*[@id="password"]').send_keys(password)
login_attempt = browser.find_element_by_xpath('/html/body/div/div[4]/div/div/div/div/div/form/button[1]')
login_attempt.submit()
print('Logged In')
time.sleep(2) # Wait for new page to load
browser.find_element_by_xpath('//*[@id="title"]').send_keys('Title') #  Code breaks here. It cannot find the title entry area because the new page is blank.

我尝试过创建 Firefox 配置文件,给页面加载时间,并关闭证书。当我手动登录时它加载得很好。提前感谢您的帮助!

【问题讨论】:

  • 你试过 login_attempt.click() 而不是 login_attempt.submit() 吗?

标签: python selenium css-selectors webdriverwait firefox-profile


【解决方案1】:

要在 url https://cmsdb.darkcosmos.org/experiments/run/new 内登录并使用新的 FirefoxProfile 提供 用户名密码,您需要诱导 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    profile = webdriver.FirefoxProfile()
    profile.accept_untrusted_certs = True
    browser = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    browser.get('https://cmsdb.darkcosmos.org/experiments/run/new')
    print('Connected to Server')
    login_button = browser.find_element_by_xpath('/html/body/div/div[5]/main/div/div[1]/div/div[3]/button')
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.v-content__wrap button.v-btn.v-btn--flat.theme--light.primary--text"))).click()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("clcarver")
    browser.find_element_by_css_selector("input#password").send_keys("clcarver")
    login_attempt = browser.find_element_by_css_selector("div.v-btn__content>i.v-icon.pr-1.mdi.mdi-lock-open-outline.theme--light").click()
    
  • 控制台输出:

    Connected to Server
    
  • 浏览器截图:

【讨论】:

    猜你喜欢
    • 2011-02-03
    • 2020-04-29
    • 2020-11-24
    • 2015-02-25
    • 2021-06-28
    • 2016-04-11
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多