简体   繁体   English

如何使用 GitHub 页面 https://github.com/ 使用 Selenium 和 ZA7F5F35426B923741BFC31 单击登录元素

[英]How to click on the Sign in element with GitHub page https://github.com/ using Selenium and Python

I'm trying to use Selenium for python to click on the sign in link at the top of github.我正在尝试将 Selenium 用于 python 以单击 github 顶部的登录链接。 I've tried using find_element_by_link_text() but i get a NoSuchElementException .我试过使用find_element_by_link_text()但我得到了NoSuchElementException I then tried using find_element_by_xpath() and I got an ElementNotinteractableException .然后我尝试使用find_element_by_xpath()并得到一个ElementNotinteractableException Here is the code for the first:这是第一个的代码:

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://github.com')

signin = browser.find_element_by_link_text('Sign in')
signin.click()

and here's the code for the second.这是第二个的代码。

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://github.com')

signin_link = browser.find_element_by_xpath('/html/body/div[1]/header/div/div[2]/div[2]/a[1]')
signin_link.click()

I even tried find_element_by_css_selector() but also got an ElementNotInteractableException .我什至尝试find_element_by_css_selector()但也得到了ElementNotInteractableException I don't understand what's going wrong.我不明白出了什么问题。 I don't feel like putting in the html, but if you go to github, it's just the sign in link at the very top right.我不想放入 html,但如果您将 go 放入 github,这只是右上角的登录链接。

I think you are missing out to pass chromedriver path.我认为您错过了传递 chromedriver 路径。 Try this:尝试这个:

browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")

Also, If you want to go to the login page, then I would recommend avoiding the long way root.另外,如果你想 go 到登录页面,那么我建议避免远程 root。 What I mean is, the following code should directly take you to the login page:我的意思是,下面的代码应该直接带你到登录页面:

browser.get('https://github.com/login') 

However, if you must know, how else you can click that element, try looping over "href" elements:但是,如果您必须知道如何单击该元素,请尝试遍历“href”元素:

for el in browser.find_elements_by_tag_name("a"):
    if "/login" in el.get_attribute('href'):
        el.click()

To handle dynamic element induce WebDriverWait () and wait for element_to_be_clickable () and use following locator strtegies.要处理动态元素,请诱导WebDriverWait () 并等待element_to_be_clickable () 并使用以下定位器策略。

LINK_TEXT : LINK_TEXT

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

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT,"Sign in")))
signin.click()

XPATH : XPATH

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.XPATH,"//a[@href='/login']")))
signin.click()

CSS Selector: CSS 选择器:

browser = webdriver.Chrome()
browser.get('https://github.com')
signin =WebDriverWait(browser,10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,"a[href='/login']")))
signin.click()

To click() on the Sign in element at the top right corner of GitHub page https://github.com/ using Selenium , you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies : To click() on the Sign in element at the top right corner of GitHub page https://github.com/ using Selenium , you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using PARTIAL_LINK_TEXT :使用PARTIAL_LINK_TEXT

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sign"))).click()
  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(., 'Sign')]"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暂无
暂无

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

相关问题 如何在 python(2.7) 中使用 Tika 包 (https://github.com/chrismatmann/tika-python) 来解析 PDF 文件? - How can I use Tika package(https://github.com/chrismattmann/tika-python) in python(2.7) to parse PDF files? 我正在使用https://github.com/scaperot/the-BPM-detector-python/blob/master/bpm_detection/bpm_detection.py,如何输入音频文件? - I am using https://github.com/scaperot/the-BPM-detector-python/blob/master/bpm_detection/bpm_detection.py, how to input the audio file? 如何在@github.com 之前删除敏感信息以使用 Python 3.9 和/或正则表达式正确清理它? - How to remove the sensitive information before @github.com to sanitize it correctly using Python 3.9 and/or regex? 代码python中的深层anpr错误?https://github.com/matthewearl/deep-anpr - deep anpr error in code python ?https://github.com/matthewearl/deep-anpr 为什么我使用 https://github.com/amosbastian/fpl 访问 FPL API 的代码会失败? - Why could my code to access the FPL API using https://github.com/amosbastian/fpl be failing? 我如何登录“ https://github.com/NoMore201/googleplay-api” - How do I login in “https://github.com/NoMore201/googleplay-api” 如何更改 .flair 缓存文件夹位置 (https://github.com/flairNLP/flair)? - How can I change the .flair cache folder location (https://github.com/flairNLP/flair)? 如何使用 Selenium 和 Python 在 https://mail.protonmail.com/ 注册页面中单击创建帐户按钮 - How to click on the Create Account button within https://mail.protonmail.com/ signup page using Selenium and Python Heroku - 上传 Django-App 时应用程序与 buildpack https://github.com/heroku/heroku-buildpack-python.git 不兼容 - Heroku - App not compatible with buildpack https://github.com/heroku/heroku-buildpack-python.git when uploading a Django-App 如何使用 selenium - Python 在 twitter 登录页面中单击下一步 - How to click on next in the twitter sign in page using selenium - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM