简体   繁体   English

如何使用 Selenium 和 Javascript 从背景图像中检索 url

[英]How to retrieve the url from background image using Selenium and Javascript

I'm trying to retrieve the url from an background image with Xpath or Javascript, but with no luck for now.我正在尝试使用 Xpath 或 Javascript 从背景图像中检索 url,但现在没有运气。 this is what I have这就是我所拥有的

<div style="width: 100%; display: inline-block;">
 <div tws-article-images-preload="item" class="tws-article-images--preload ng-scope tws-article-images-- 
   image-small" ng-click="closeImage()" use-original="useOriginal" style="background-image: 
   url(&quot;https://i.pinimg.com/originals/59/54/b4/5954b408c66525ad932faa693a647e3f.jpg;);">
 </div>
</div>

And have tried this two with different modification并尝试了这两种不同的修改

//*[starts-with(@style, 'background-image: url()] <--Did not work 

return document.getElementsByClassName('tws-article-images--preload ng-scope tws-article-images--image-small').src
document.getElementsByClassName('classname')[0].style.backgroundImage.slice(4, -1).replace(/"/g, "");

You can try你可以试试

return document.getElementsByClassName('tws-article-images--preload ng-scope tws-article-images--image-small').style.backgroundImage.slice(4, -1).replace(/["']/g, "");

Your XPath expression is wrong (missing ' and a possible annoying CRLF ).您的 XPath 表达式是错误的(缺少'和可能令人讨厌的CRLF )。 Fix it with:修复它:

//*[starts-with(@style, 'background-image:')]/@style

Then use regex to clean the result:然后使用正则表达式清理结果:

txt = 'background-image: url("https://i.pinimg.com/originals/59/54/b4/5954b408c66525ad932faa693a647e3f.jpg;);'
result = re.sub(r"^.+\"(.+?);.+", r"\1", txt)
print(result)

With XPath:使用 XPath:

substring-after(substring-before(//*[starts-with(@style, 'background-image:')]/@style,';)'),'"')

Output: https://i.pinimg.com/originals/59/54/b4/5954b408c66525ad932faa693a647e3f.jpg Output: https://i.pinimg.com/originals/59/54/b4/5954b408c66525ad932faa693a647e3f.jpg

To retrieve the url from the background image you can usethe following solution:要从背景图像中检索 url,您可以使用以下解决方案:

  • Using slice() :使用slice()

     var img = document.getElementsByClassName('tws-article-images--preload')[0], style = img.currentStyle || window.getComputedStyle(img, false), bgImage = style.backgroundImage.slice(4, -1).replace(/"/g, ""); //printing the url console.log('Image URL: ' + bgImage);
  • Using regex :使用正则表达式

     var img = document.getElementsByClassName('tws-article-images--preload')[0], style = img.currentStyle || window.getComputedStyle(img, false), var url = backgroundImage.match(/url\(["']?([^"']*)["']?\)/)[1]; //printing the url console.log('Image URL: ' + url);

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

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