如何通过Selenium python登录yahoo(尝试了多种解决方案)

2024-04-19 11:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过selenium登录我的雅虎帐户。我只是在学习这一点,并为不同的网站,以获得更舒适的硒整体程序。你知道吗

我现在正试图登录雅虎,但我还没弄明白。你知道吗

网址:https://login.yahoo.com/

我试过做:

yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "#login-username"))).sendKeys("tester@yahoo.com")

yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "login-username"))).sendKeys("tester@yahoo.com")

yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@id='login-username']"))).sendKeys("tester@yahoo.com")

yahoologin1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/form[1]/div[2]/input[1]"))).sendKeys("tester@yahoo.com")

有什么我特别缺的吗?如果有人告诉我如何让这个登录生效,我将不胜感激。与其简单的复制粘贴代码:)谢谢!你知道吗

我做了一些进一步的测试,它在主选项卡上工作。然而,我打开一个新的标签与雅虎登录页它不工作。我是否需要做一些不同的事情才能在新标签上键入内容?你知道吗


Tags: ofbrowsercombyloginelementyahoountil
2条回答

我用了另一种没有WebDriverWait的方法来解决,希望对你有帮助。你知道吗

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(" window-size=1920x1080")
browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)

# Opening yahoo page in a new tab
browser.execute_script("window.open('https://login.yahoo.com/');")
# Switch to new tab
browser.switch_to.window(browser.window_handles[-1])
# Selecting login-username and putting email
browser.find_element_by_id('login-username').send_keys('tester@yahoo.com')

结果:

Result

下面是示例脚本。你知道吗

url = "https://login.yahoo.com/"
# Step 1 -navigate to the AUT
driver.get(url)
print ("Step 1 - Done")
# Step 2 - Enter the username
#wait for the user name to be displayed
userName = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'input#login-username')))
userName.send_keys("used css")
# Step 3 - click on Next
driver.find_element_by_xpath("//input[@id='login-signin']").click()
# Step 4 - Enter password
passWord = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='password']")))
passWord.send_keys("password")
# Step 5 - Click on Sign in
driver.find_element_by_id("login-signin").click()

下面是如何快速有效地进行脚本开发。你知道吗

  • 在浏览器导航到url后保留断点 enter image description here
  • 使用chrome开发工具获取元素xpath,请参考here以了解如何获取测试和xpath
  • 转到控制台>;单击show python prompt打开交互式控制台 enter image description here
  • 在此处输入代码,然后按Enter键检查该行在脚本中放置时是否有效 enter image description hereenter image description here

  • 进行任何必要的更改并确认步骤正常 enter image description here

  • 将步骤从交互式控制台复制粘贴到脚本 enter image description here

相关问题 更多 >