简体   繁体   English

使用 Selenium 登录《华尔街日报》时发生错误

[英]Error occurs using Selenium to logging into WSJ

I am trying to write a program to automatically log into WSJ.com with my own login.我正在尝试编写一个程序,用我自己的登录名自动登录到 WSJ.com。 I've looked at some of the other guy's codes, but none of them seemed to have the problem where when click the Login link on the WSJ.com main page, it takes you to two potential pages:我看过其他人的一些代码,但似乎没有一个问题,当单击 WSJ.com 主页上的登录链接时,它会将您带到两个潜在页面:

  1. Ask for user name and password.询问用户名和密码。
  2. Ask for user name, once hit continue brings you to the page mentioned above.询问用户名,一旦点击继续将您带到上面提到的页面。

For the 2nd scenario, I have managed to input username and password, then hit login.对于第二种情况,我已经设法输入用户名和密码,然后点击登录。 However that brings me to the next page where it askes if I want to verify my email or continue to WSJ.但是,这将我带到下一页,询问我是否要验证我的电子邮件或继续阅读《华尔街日报》。 In my code, I can't seem to get the webdriver to locate the button that says "Continue to WSJ".在我的代码中,我似乎无法让 webdriver 找到显示“继续阅读华尔街日报”的按钮。 If I hit F12 under developer tools, I can locate the button.如果我在开发人员工具下按 F12,我可以找到按钮。

Code below:代码如下:

try:
    submit_button = driver.find_element_by_xpath(".//button[@type='button'][@class='solid-button continue-submit new-design']")
    username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
    user1 = "username"
    pass1 = "password"
    username.send_keys(user1)
    submit_button = driver.find_element_by_xpath(".//button[@type='button'][@class='solid-button continue-submit new-design']")
    submit_button.click()
    password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'password')))
    password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
    username.send_keys(user1)
    password.send_keys(pass1)
    driver.get(url)
    submit_button = driver.find_element_by_xpath(".//button[@type='button'][@class='solid-button new-design basic-login-submit']")
    submit_button.click()
except:
    username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
    password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
    user1 = "username"
    pass1 = "password"
    username.send_keys(user1)
    password.send_keys(pass1)
    submit_button = driver.find_element_by_xpath(".//button[@type='submit'][@class='solid-button basic-login-submit']")
    submit_button.click()
    continue_button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "solid-button reg-rtl-btn")))
    continue_button.click()

For my code when exception occurs, especially trying to locate the "solid-button reg-rtl-btn" button, it gives me the below message:对于发生异常时的代码,尤其是尝试定位“solid-button reg-rtl-btn”按钮时,它给了我以下消息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".solid-button reg-rtl-btn"}

When stringing together classes in a class name selector, they need to get separated by ., which says the next thing coming is also a class name (the first one is automatically a class because you are searching by class).在类名选择器中将类串在一起时,它们需要用 . 分隔,这表示接下来出现的也是类名(第一个自动是类,因为您是按类搜索)。

Thus solid-button.reg-rtl-btn will locate by class name and .solid-button.reg-rtl-btn would locate by CSS selector, but having the space in between meant it did not know how to interpret reg-rtl-button .因此, solid-button.reg-rtl-btn将通过类名定位, .solid-button.reg-rtl-btn将通过 CSS 选择器定位,但中间有空格意味着它不知道如何解释reg-rtl-button There are more complicated ways to do this, too, but I do not see why you would want to (maybe if you were worried about the order in which the classes could appear).还有更复杂的方法可以做到这一点,但我不明白你为什么想要(也许如果你担心类出现的顺序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM