简体   繁体   English

Python if elif else 无法进入elif语句Selenium

[英]Python if elif else can't go to the elif statement Selenium

I have a piece of code that checks if there is an element that is displayed using p then it shows a dialog box but if it can't find it then it checks for another element and if that is displays another dialog box and if that element is not available, then it runs a piece of code.我有一段代码检查是否有使用 p 显示的元素,然后它显示一个对话框,但如果找不到它,则它检查另一个元素,如果它显示另一个对话框,并且该元素不可用,然后它运行一段代码。

The code is here.代码在这里。

if driver.find_element_by_class_name('error-code').is_displayed():
    LoginError.show()
elif driver.find_element_by_xpath('//*[@id="sample-data-table"]/thead').is_displayed():
    QuestionUnavailable.show()
else:
    if driver.find_element_by_xpath('//*[@id="radio1"]').is_displayed():
        driver.find_element_by_xpath('//*[@id="radio1"]').click()
        driver.find_element_by_xpath('//*[@id="btn-savecheck"]').click()
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="btn-alert-ok"]').click()

But when I run it, I get this error但是当我运行它时,我收到此错误

File "Sciborg.py", line 60, in This
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".error-code"}

As you can clearly see from the error, your code fails to locate element located by error-code class name.从错误中可以清楚地看到,您的代码无法定位由error-code类名定位的元素。
This may be caused by several issues:这可能是由几个问题引起的:

  1. You are using a wrong locator, there is no element with error-code class name attribute on that page.您使用了错误的定位器,该页面上没有具有error-code类名称属性的元素。
  2. You are missing a wait / delay.您错过了等待/延迟。 Possibly you are trying to access this element before the page is loaded.可能您正试图在页面加载之前访问此元素。
  3. Maybe there is an iframe there so that element is inside that iframe so you have to switch to that iframe in order to access the desired element.也许那里有一个 iframe,因此该元素位于该 iframe 内,因此您必须切换到该 iframe 才能访问所需的元素。
    UPD UPD
    In case there is no such element on the page you can use find_elements instead of find_element method like the following:如果页面上没有这样的元素,您可以使用find_elements代替find_element方法,如下所示:
if driver.find_elements_by_class_name('error-code'):
    LoginError.show()
elif driver.find_elements_by_xpath('//*[@id="sample-data-table"]/thead'):
    QuestionUnavailable.show()
else:
    if driver.find_elements_by_xpath('//*[@id="radio1"]'):
        driver.find_element_by_xpath('//*[@id="radio1"]').click()
        driver.find_element_by_xpath('//*[@id="btn-savecheck"]').click()
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="btn-alert-ok"]').click()

driver.find_elements_by_class_name('error-code') will return a list of web elements. driver.find_elements_by_class_name('error-code')将返回网络元素列表。 If element is existing this will return a non-empty list that is interpreted as a boolean true .如果元素存在,这将返回一个非空列表,该列表被解释为布尔值true Otherwise it will return an empty list that is interpreted as a boolean false .否则,它将返回一个被解释为布尔值false的空列表。
Similarly I have updated the other if cases.同样,我更新了另一个if案例。
But you still should insure the elements you are trying to access directly are existing.但是您仍然应该确保您尝试直接访问的元素存在。
I mean these elements:我的意思是这些元素:

driver.find_element_by_xpath('//*[@id="radio1"]').click()
driver.find_element_by_xpath('//*[@id="btn-savecheck"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="btn-alert-ok"]').click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM