![](/img/trans.png)
[英]How can I use Tika package(https://github.com/chrismattmann/tika-python) in python(2.7) to parse PDF files?
[英]How to click on the Sign in element with GitHub page https://github.com/ using Selenium and Python
我正在尝试将 Selenium 用于 python 以单击 github 顶部的登录链接。 我试过使用find_element_by_link_text()
但我得到了NoSuchElementException 。 然后我尝试使用find_element_by_xpath()
并得到一个ElementNotinteractableException 。 这是第一个的代码:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://github.com')
signin = browser.find_element_by_link_text('Sign in')
signin.click()
这是第二个的代码。
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()
我什至尝试find_element_by_css_selector()
但也得到了ElementNotInteractableException 。 我不明白出了什么问题。 我不想放入 html,但如果您将 go 放入 github,这只是右上角的登录链接。
我认为您错过了传递 chromedriver 路径。 尝试这个:
browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")
另外,如果你想 go 到登录页面,那么我建议避免远程 root。 我的意思是,下面的代码应该直接带你到登录页面:
browser.get('https://github.com/login')
但是,如果您必须知道如何单击该元素,请尝试遍历“href”元素:
for el in browser.find_elements_by_tag_name("a"):
if "/login" in el.get_attribute('href'):
el.click()
要处理动态元素,请诱导WebDriverWait
() 并等待element_to_be_clickable
() 并使用以下定位器策略。
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 :
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 选择器:
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 :
使用PARTIAL_LINK_TEXT
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sign"))).click()
使用CSS_SELECTOR
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()
使用XPATH
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(., 'Sign')]"))).click()
注意:您必须添加以下导入:
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.