繁体   English   中英

使用 Selenium 查找具有给定 src 链接的图像并单击

[英]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>
IMAGELINK = "http://www.blablabla.com/justapicture.jpg"

我的目标是在特定页面中使用 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.

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