Reputation: 112
I have one HTML string which I am opening in browser from PhanthomJs and trying to save the outcome URL.
My code looks as below.
driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
element = driver.find_element_by_tag_name('body')
elem = element.find_element_by_tag_name('noscript')
print elem.find_element_by_tag_name('img')
The html_content
look like below.
<script language="javascript" src="https://somejs"></script>
<noscript>
<a href="https://track.adform.net/C/?bn=15864640;C=0" target="_blank">
<img src="https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]" border="0" width="728" height="90" alt=""/>
</a>
</noscript>
Above html render an image when I ran it locally as html file.
I wanted to save that image with the code I have mentioned above. But unfortunately I am not able to find the img using find_element_by_tag_name
I am getting below error.
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with tag name 'img'","request"
Please let me know what might I am doing wrong here.
Upvotes: 1
Views: 83
Reputation: 4315
Try this, if simple HTML content not web page content
from bs4 import BeautifulSoup
html_content = """ <script language="javascript" src="https://somejs"></script>
<noscript>
<a href="https://track.adform.net/C/?bn=15864640;C=0" target="_blank">
<img src="https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]" border="0" width="728" height="90" alt=""/>
</a>
</noscript>"""
sp = BeautifulSoup(html_content,'html.parser')
elem = sp.find('noscript')
img = elem.find('img')
print(img['src'])
O/P:
https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]
scrape web page content by website url:
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.get('http://www.test.com')
sp = BeautifulSoup(driver.page_source,'html.parser')
elem = sp.find('noscript')
img = elem.find('img')
print(img['src'])
Where "/usr/bin/chromedriver"
chrome drive path
Upvotes: 1