[英]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.