[英]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:这可能是由几个问题引起的:
error-code
class name attribute on that page.error-code
类名称属性的元素。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.