Collect image link with python , Selenium and webdriver

Asked

Viewed 850 times

-1

I need to run a python script to get the url of an image that is only shown after running the page’s javascript. I am using python3 with Selenium and webdriver. However when trying to locate the link and print this with error. Follows html print and script used.

    import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import selenium.webdriver.chrome.service as service
from pyvirtualdisplay import Display
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.common.exceptions import TimeoutException

chromedriver_path = "/usr/bin/chromedriver"
chromium_path = "/usr/bin/chromium-browser"

service = service.Service(chromedriver_path)
service.start()
capabilities = {'chrome.binary': chromium_path}
opts = Options()
opts.binary_location = chromium_path

display = Display(visible=0, size=(800, 800))  
display.start()

driver = webdriver.Chrome()
driver = webdriver.Chrome(chrome_options=opts)
driver.get('http://IP/devices/')
timeout = 20
image = browser.find_elements_by_xpath(("//img")
print(image, '\n')

codigo htlm

1 answer

0

If I’m not mistaken, the find_elements_by_xpath returns a list of all elements that obey xpath. Try to use the find_element_by_xpath passing the xpath using the ID, thus: browser.find_element_by_xpath("//img[@id = 'imgcap']") or use the method that already looks directly for the ID: find_element_by_id.

NOTE: In this case, if you pass only //img in xpath, it will return the first node of type img.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.