0

创建一个脚本,该脚本将登录到下面的站点并自动将值记录到 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 配置文件,给页面加载时间,并关闭证书。当我手动登录时它加载得很好。在此先感谢您的帮助!

4

1 回答 1

0

要在 url中登录并使用新的FirefoxProfilehttps://cmsdb.darkcosmos.org/experiments/run/new提供用户名密码,您需要诱导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
    
  • 浏览器截图:

DarkCosmos_登录

于 2018-10-16T07:22:58.023 回答