[英]How to find and click an image link using the image's src (Selenium, Python)
[英]Using Selenium to find an image with a given src link and click
这是我要抓取的网站的 html 代码
<div> <div class="activityinstance"> <a class="" onclick="" href="https://www.blablabla.com"> <img src="http://www.blablabla.com/justapicture.jpg" class="iconlarge activityicon" alt="" role="presentation" aria-hidden="true"> <span class="instancename">title<span class="accesshide "> text </span> </span> </a> </div> </div>
我的目标是在特定页面中使用 python 找到与 IMAGELINK 关联的所有 href。 这个网址中的图片往往会显示多次,我想收到所有链接,以便我可以点击它们。
我尝试按类名“a”查找元素以提取页面中的所有链接,这样如果我能找到它们的 xPath,我就可以格式化“/img”并从该元素获取属性“src”。 但问题是我还没有找到用给定的 webdriver 元素提取 xPath 的方法。
注意:除非我编写一些函数来生成它,否则我无权访问元素的 Xpath
找到所有带有img
标签的元素并打印src
属性:
imgs = driver.find_elements_by_xpath("//img")
for img in imgs:
print(img.get_attribute("src"))
我认为他想要父 href 的 img src 等于
imgs = driver.find_elements_by_xpath("//img[src='http://www.blablabla.com/justapicture.jpg']/parent::a")
for img in imgs:
print(img.get_attribute("href"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.