1

I'm new in Selenium and Python. I have the following code:

<div class="Product_ImageWrapper__3W7pX">
        <span class=" lazy-load-image-background opacity lazy-load-image-loaded" style="color: 
         transparent; display: inline-block; height: 100%; width: 100%;">
            <img class="Product_Image__1sMZc" src="xxx.jpg" alt="photo" height="100%" 
             width="100%"></span></div> 

What I'm trying to do is click on this image. There is always message:

Message: no such element: Unable to locate element:......

I tried:

driver.find_element_by_xpath('//span[img/@src="xxx.jpg"]').click()

or just by the name of the class but it doesn't work. Please help.

0

2 Answers 2

2

Try the execute() function. It executes the code directly in the JavaScript Console.

driver.execute("document.getElementsByClassName(' lazy-load-image-background opacity lazy-load-image-loaded')[0].click()")

IF that did not work out, There is a second method using the WebdriverWait().

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'span[class=" lazy-load-image-background opacity lazy-load-image-loaded"]')))
Sign up to request clarification or add additional context in comments.

5 Comments

still doesn't work. The first solution throws out the error: KeyError: "document.getElementsByClassName(' lazy-load-image-background opacity lazy-load-image-loaded')[0].click()" and the second is executed, but the element doen't get clicked.
@TigerJ I have forgotten to add click() in the second one. Try that WebDriverWait(self.webdriver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'span[class=" lazy-load-image-background opacity lazy-load-image-loaded"]'))).click()
After clicking on the picture, a small window should pop up. This time I see that it finds an element (there is no error), but the window after clicking doesn't show up.
@TigerJ so try to click another element - I mean I tried to click span, so try to use 'img[class="Product_Image__1sMZc"]'.
'img[class="Product_Image__1sMZc"] it works for me. Thanks for your help
1

Please use following xpath "//img[@src='xxx.jpg']

Before to click you should wait for that element

3 Comments

Still get this message: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element . And yes, I wait a few seconds each time for an element to show up.
Just got the solution.
if my answer usefull to you upvote answer so that others can get idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.