Selenium如何通过src查找包含字符串的图像

时间:2019-08-29 14:47:12

标签: python selenium

我在硒页面上找不到图像。我的脚本在python中。

    try:
        is_captcha = driver.find_element_by_css_selector("img[src*='validation']")
        if is_captcha:
            print("CAPTCHA FOUND!")
    except:
        try:
            is_captcha = driver.find_element_by_css_selector("[src*='validation']")
            if is_captcha:
                print("CAPTCHA FOUND!")
        except:
            print ("No catpcha form for {}".format(link_hyip))

我的图片所在的HTML代码:

<td class="menutxt"><img src="?a=show_validation_image&amp;PHPSESSID=u020264onvkgu0fsl9b94lk1v1&amp;rand=1901750303"></td>

我收到此错误:

no such element: Unable to locate element: {"method":"css selector","selector":"[src*='validation']"}
  (Session info: chrome=76.0.3809.132)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用WebdriverWaitvisibility_of_element_located以及下面的xpath。

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

driver = webdriver.Chrome()
driver.get('https://www.lanysoft.biz/?a=support')
try:
  is_captcha =WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//form[@name='loginform']//img[contains(@src,'show_validation')]"))) 
  if is_captcha:
    print("CAPTCHA FOUND!")
except:
    print('recapcha not found')