Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Selenium和Python从元素获取链接_Python_Selenium - Fatal编程技术网

如何使用Selenium和Python从元素获取链接

如何使用Selenium和Python从元素获取链接,python,selenium,Python,Selenium,假设一个网页中的所有Author/username元素如下所示。。。 如何使用python和Selenium访问href部分? 用户=浏览器。通过xpath(?)查找元素 作者: 谢谢。使用按标签名称(“a”)查找元素来查找“a”标签,然后使用获取属性('href')来获取链接字符串。使用//span[contains(text(),“Author”)]/a作为xpath表达式 例如: from selenium import webdriver driver = webdriver.Fir

假设一个网页中的所有Author/username元素如下所示。。。 如何使用python和Selenium访问href部分? 用户=浏览器。通过xpath(?)查找元素


作者:

谢谢。

使用
按标签名称(“a”)查找元素
来查找“a”标签,然后使用
获取属性('href')
来获取链接字符串。

使用
//span[contains(text(),“Author”)]/a
作为xpath表达式

例如:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://jsfiddle.net/9pKMU/show/')
for a in driver.find_elements_by_xpath('.//span[contains(text(), "Author")]/a'):
    print(a.get_attribute('href'))

难道我不需要解析页面上的所有链接,然后找到包含“account”的链接吗?我怎样才能找到作者:?@jacob501啊,我错过了条件。如果你想找到“帐户”链接,beautifulsoup非常有帮助。您可以这样使用它:
soup.findAll('span',text=re.compile(r'Author:')
找到目标“span”,然后
find('a').attrs['href']
获取链接。它更具可读性。啊,我忘了一件事,beautifulsoup只用于页面解析,如果您想在selenium中执行一些操作,falsetru的答案更好。
URL=driver.find_element_by_tag_name('a')。get_attribute('href')是如何将其作为字符串返回的。
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://jsfiddle.net/9pKMU/show/')
for a in driver.find_elements_by_xpath('.//span[contains(text(), "Author")]/a'):
    print(a.get_attribute('href'))