所有img的Selenium Python Webdriver chrome print src's

2024-09-24 02:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我要打印页面中所有的img的src,我想看看它是否能识别它, 因为当我这么做的时候

driver.find_element_by_xpath("//img[@src='http://images.yad2.co.il/Pic/site_images/yad2/MyYad2/images/myorderbottom/new/jump_ad.png'").click()

上面写着:

^{pr2}$

我尝试过css_selector,但是没有成功,所以我想打印imgs的所有src,我该怎么做呢?在


Tags: srchttpimgbydriversite页面element
2条回答

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //img[@src='http://images.yad2.co.il/Pic/site_images/yad2/MyYad2/images/myorderbottom/new/jump_ad.png' because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[@src='http://images.yad2.co.il/Pic/site_images/yad2/MyYad2/images/myorderbottom/new/jump_ad.png'' is not a valid XPath expression.

实际上,您的xpath在语法上不正确,您缺少右括号],因此您应该尝试如下:

driver.find_element_by_xpath(".//img[@src='http://images.yad2.co.il/Pic/site_images/yad2/MyYad2/images/myorderbottom/new/jump_ad.png']").click()

或使用css_selector作为:-

^{pr2}$

如果要打印所有imgsrc属性,请尝试如下方式:-

images = driver.find_elements_by_tag_name("img")
for image in images :
  print(image.get_attribute("src"))

好吧,@Saurabh Gaur是对的,现场有Iframe, 简单的解决方案

driver.switch_to.frame(driver.find_element_by_id("FrameID"))

从那里我可以找到按钮,然后我就回来了

^{pr2}$

从这里我可以继续到下一帧了! @苏拉布·高尔非常感谢你!,你太棒了!在

相关问题 更多 >