我正在尝试通过 this link 登录我的帐户通过 Python 并使用 Selenium 包在市场观察网页上。我可以选择“登录”按钮并单击它,但没有任何反应,因此无法访问我想要的网页。
我认为我的错误来自于我可能没有选择正确的按钮。元素代码如下:
<div class="sign-in">
<button class="solid-button basic-login-submit">
<span class="text" data-token="signIn" token-title="signIn" title="Sign In">
Sign In
</span>
</button>
</div>
<div class="create-connect" style="display: none;">
<button class="solid-button basic-login-connect">
<span class="text" data-token="yesConnect">
Yes, connect
</span>
</button>
...some other buttons for facebook connection
我的代码如下:
driver.get("https://sso.accounts.dowjones.com/login?state=g6Fo2SBQQURPVkxkcTR0RW1zX21NSmxLY3F4Q2RoUm5QTWV4UaN0aWTZMmdhRm8yU0IwUW01dVV6WjJiVXhVUmtaRVRXd3dlamM1UVdjNVIweExiVFJVYlc5WFl3o2NpZNkgNWhzc0VBZE15MG1KVElDbkpOdkM5VFhFdzNWYTdqZk8&client=5hssEAdMy0mJTICnJNvC9TXEw3Va7jfO&protocol=oauth2&prompt=login&scope=openid%20idp_id%20given_name%20family_name%20email%20djid%20prts&response_type=code&redirect_uri=https%3A%2F%2Faccounts.marketwatch.com%2Fauth%2Fsso%2Flogin&nonce=ac689d94-8b8f-4e6d-9255-439f58fc3c0f&connection=DJldap&ui_locales=en-us-x-mw-3-8&ns=prod%2Faccounts-mw#!/signin") #this is the link above
username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("myemail@hotmail.com")
password.send_keys("***")
driver.find_element_by_xpath(".//*[contains(text(), 'Sign In')]").click()
driver.find_element_by_xpath(".//*[contains(text(), 'Yes, connect')]").click()
第一个元素应该位于登录按钮上的位置。我不确定第二次点击,但我仍然尝试了,但仍然没有发生任何事情。
我有兴趣了解:
- 我做错了什么
- 为什么当我点击“登录”按钮时没有任何反应
- 如何克服它
我也尝试过,但没有成功:
driver.find_element_by_css_selector("solid-button.basic-login-submit")
最佳答案
你的尝试非常接近。将您尝试的代码更改为
# You'll need to import these dependencies
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button = WebDriverWait(driver,1000).until(By.CSS_SELECTOR,"button.solid-button.basic-login-submit")
button.click()
WebDriverWait(0,1000)
使程序搜索元素 1000 秒。这更好,因为它给了页面加载时间。
关于python - 通过 Python 中的 Selenium 选择按钮登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55542203/