[英]Selenium Python unable to find_element_by_link_text
I'm trying to use Selenium in conjunction with BeautifulSoup. 我正在尝试将Selenium与BeautifulSoup结合使用。 To be more specific, I'm trying to click the link
NCAA Division I
from the drop down menu here (initially it says Top 25
). 更具体地说,我尝试从此处的下拉菜单中单击链接
NCAA Division I
(最初显示为Top 25
)。
The HTML tag for the link looks like this: 链接的HTML标签如下所示:
<li><a href="#" data-type="conference" data-week="null-null-null" data-group="50">NCAA Division I</a></li>
To me, it seems straight forward: driver.find_element_by_link_text('NCAA Division I').click()
在我看来,这似乎很简单:
driver.find_element_by_link_text('NCAA Division I').click()
Is this because href="#"
or maybe because the <a>
tag is embedded in a <li>
tag? 这是因为
href="#"
还是因为<a>
标签嵌入在<li>
标签中?
Here's what happens: 这是发生了什么:
(Pdb) driver.find_element_by_link_text('NCAA Division I').click()
*** NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"NCAA Division I"}
(Session info: chrome=54.0.2840.100)
(Driver info: chromedriver=2.25.426924 (649f9b868f6783ec9de71c123212b908bf3b232e),platform=Linux 4.4.0-45-generic x86_64)
Full code: 完整代码:
import numpy as np
import requests
from bs4 import BeautifulSoup
import re
import datetime
import os
from selenium import webdriver
scores_link = "http://www.espn.com/mens-college-basketball/scoreboard/_/date/20161122"
r = requests.get(scores_link)
driver = webdriver.Chrome()
driver.get(scores_link)
driver.find_element_by_link_text('NCAA Division I').click()
soup = BeautifulSoup(driver.page_source)
Since the "NCAA Division I" will only come after mouse hover, so we have to use ActionChains from selenium( http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains ) 由于“ NCAA Division I”只会在鼠标悬停之后出现,因此我们必须使用硒中的ActionChains( http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains )
try this code, it will work: 试试这个代码,它将起作用:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.espn.com/mens-college-basketball/scoreboard/_/date/20161122')
el2=driver.find_element_by_xpath("//header[@class='automated-header']//button[.='Top 25']")
action = ActionChains(driver)
action.move_to_element(el2).click().perform()
driver.find_element_by_link_text('NCAA Division I').click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.