Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Selenium python中单击元素?_Python_Selenium - Fatal编程技术网

如何在Selenium python中单击元素?

如何在Selenium python中单击元素?,python,selenium,Python,Selenium,我对Selenium和Python是新手。我有以下代码: <div class="Product_ImageWrapper__3W7pX"> <span class=" lazy-load-image-background opacity lazy-load-image-loaded" style="color: transparent; display: inline-block; heig

我对Selenium和Python是新手。我有以下代码:

<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> 
或者只是通过类的名称,但它不起作用。请提供帮助。

尝试
execute()
函数。它直接在
JavaScript
控制台中执行代码

driver.execute("document.getElementsByClassName(' lazy-load-image-background opacity lazy-load-image-loaded')[0].click()")
如果不起作用,还有第二种方法使用
WebdriverWait()

尝试
execute()
函数。它直接在
JavaScript
控制台中执行代码

driver.execute("document.getElementsByClassName(' lazy-load-image-background opacity lazy-load-image-loaded')[0].click()")
如果不起作用,还有第二种方法使用
WebdriverWait()


请使用以下xpath“//img[@src='xxx.jpg']


单击之前,您应该等待该元素

请使用以下xpath“//img[@src='xxx.jpg']


在单击之前,您应该等待该元素仍然不起作用。第一个解决方案抛出错误:KeyError:“document.GetElementsByCassName('lazy load image background opacity lazy load image loaded')[0]。单击()”,然后执行第二个解决方案,但没有单击该元素。@TigerJ我忘记在第二个解决方案中添加
单击()
。尝试
WebDriverWait(self.webdriver,10)。直到(EC.element\u to\u可点击((By.CSS\u SELECTOR,'span[class=“lazy-load-image-background-opacity lazy-load-image-load”])。点击()
点击图片后,会弹出一个小窗口。这次我看到它找到了一个元素(没有错误),但单击后的窗口没有显示。@TigerJ所以试着单击另一个元素-我的意思是我试着单击span,所以试着使用
'img[class=“Product\u Image\uu 1sMZc]”
'img[class=“Product\u Image\uu 1sMZc]”它对我有效。谢谢你的帮助,但还是不行。第一个解决方案抛出错误:KeyError:“document.GetElementsByCassName('lazy load image background opacity lazy load image loaded')[0]。单击()”,然后执行第二个解决方案,但没有单击该元素。@TigerJ我忘记在第二个解决方案中添加
单击()
。尝试
WebDriverWait(self.webdriver,10)。直到(EC.element\u to\u可点击((By.CSS\u SELECTOR,'span[class=“lazy-load-image-background-opacity lazy-load-image-load”])。点击()
点击图片后,会弹出一个小窗口。这次我看到它找到了一个元素(没有错误),但单击后的窗口没有显示。@TigerJ所以试着单击另一个元素-我的意思是我试着单击span,所以试着使用
'img[class=“Product\u Image\uu 1sMZc]”
'img[class=“Product\u Image\uu 1sMZc]”它对我有效。感谢您的帮助仍然收到以下消息:selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:无法找到元素。是的,我每次都会等待几秒钟,等待一个元素出现。我得到了解决方案。如果我的答案对您有用,请向上投票,以便其他人可以得到我的答案。我将得到以下消息:selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:无法找到元素。是的,我每次都会等待几秒钟,等待一个元素出现。我得到了解决方案。如果我的答案对你有用,请把答案投上去,这样其他人就可以知道了
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"]')))