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.
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"}
CodePudding user response:
As you can clearly see from the error, your code fails to locate element located by error-code
class name.
This may be caused by several issues:
- You are using a wrong locator, there is no element with
error-code
class name attribute on that page. - You are missing a wait / delay. Possibly you are trying to access this element before the page is loaded.
- 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.
UPD
In case there is no such element on the page you can usefind_elements
instead offind_element
method like the following:
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. If element is existing this will return a non-empty list that is interpreted as a boolean true
. Otherwise it will return an empty list that is interpreted as a boolean false
.
Similarly I have updated the other if
cases.
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()