简体   繁体   English

硒如何从属性中提取href

[英]Selenium how to extract href from attributes

<div class="turbolink_scroller" id="container">
 <article><div class="inner- article">
  <a style="height:81px;" href="LINK TO EXTRACT">
   <img width="81" height="81" src="//image.jpg" alt="code" />

Hello! 你好! I'm pretty new to selenium and I've been playing around with how to get sources for my webdriver. 我对硒很陌生,我一直在研究如何获取Webdriver的资源。 So far, I'm trying to extract a href link given an alt code as above and I'm not sure if the documentation has a means to do this. 到目前为止,我正在尝试使用上述的alt代码提取href链接,但不确定文档是否可以执行此操作。 I'm feeling that the answer is find_by_xpath but I'm not entirely sure. 我感觉答案是find_by_xpath,但我不确定。 Thank you for any tips! 谢谢您的提示!

The way is as follows 方式如下

href = driver.find_element_by_tag_name('a').get_attribute('href')

of course, you may have a lot of 'a' tags in a page, so you may make the path to your respective tag, 当然,您的页面中可能会有很多“ a”标签,因此您可能会找到相应标签的路径,

eg 例如

div = driver.find_element_by_id('container')
a = div.find_element_by_tag_name('a')
href = a.get_attribute('href')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM