gpt4 book ai didi

python - 检查元素是否存在selenium python

转载 作者:行者123 更新时间:2023-12-04 08:26:58 25 4
gpt4 key购买 nike

我正在接受 selenium python 培训,我可以做登录字段,并且需要在登录过程成功后检查特定元素。元素是这样定义的类名

if len(driver.find_element(By.CLASS_NAME, 'userIDSection').text)==30:
print('Successful Login')
break

如果登录成功我没有得到错误,但失败时我得到“没有这样的元素:无法定位元素”。如何正确检查元素以跳过此类错误?

** 尝试这段代码

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).click()
time.sleep(3)
if len(driver.find_elements(By.CLASS_NAME, 'userIDSection'))>0:
print('Successful Login')
break
print('Login Failure')

我已经能够登录,但在这一行出现错误

TimeoutException                          Traceback (most recent call last)
<ipython-input-44-1f801f6ca398> in <module>
28 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "txtCaptcha"))).send_keys(captcha)
29
---> 30 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).click()
31 time.sleep(3)
32 if len(driver.find_elements(By.CLASS_NAME, 'userIDSection'))>0:

C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
78 if time.time() > end_time:
79 break
---> 80 raise TimeoutException(message, screen, stacktrace)
81
82 def until_not(self, method, message=''):

TimeoutException: Message:

检查元素时的代码应该打印“登录成功”并中断以 while True: 开头的循环

** 这是完整的代码,它可以工作并且登录正常,但我认为在断点之后出现错误

from selenium import webdriver 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
import pytesseract
from PIL import Image
import time

def getCaptcha(img):
pytesseract.pytesseract.tesseract_cmd=r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img=Image.open(img)
text=pytesseract.image_to_string(img, lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
return text

driver = webdriver.Chrome("C:/chromedriver")
driver.get("https://eservices.moj.gov.kw/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "headerTabLeft"))).click()

while True:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "txtUserID"))).send_keys("username")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "txtPWD"))).send_keys("password")

imgName = 'Output.png'
imgElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='frmLogin']/table[1]/tbody/tr[1]/td/img")))
imgElement.screenshot(imgName)
time.sleep(5)
captcha = getCaptcha(imgName)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "txtCaptcha"))).send_keys(captcha)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).click()
time.sleep(3)

try:
element = driver.find_element(By.CLASS_NAME, 'userIDSection')
break
except:
#except NoSuchElementException:
print("No Element Found")

print('Successful Login')

最佳答案

使用.find_elements(注意复数)方法:

if len(driver.find_elements(By.CLASS_NAME, 'userIDSection'))>0:
# DO YOUR THING

因此,如果不存在具有该类名的元素,您将得到 len = 0,但不会出错。

关于python - 检查元素是否存在selenium python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65212611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com