Reputation: 6930
Here is my code:
ignore_btn_status = driver.find_element_by_css_selector("body > div.ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-dialog-buttons.ui-draggable.ui-resizable > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)")
if (ignore_btn_status):
ignore_btn_status[0].click()
But I am getting error:
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-114-d9f8d3835039> in <module>
27 # pass
28
---> 29 ignore_btn_status = driver.find_element_by_css_selector("body > div.ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-dialog-buttons.ui-draggable.ui-resizable > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)")
30 if (ignore_btn_status):
31 ignore_btn_status[0].click
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_css_selector(self, css_selector)
596 element = driver.find_element_by_css_selector('#foo')
597 """
--> 598 return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
599
600 def find_elements_by_css_selector(self, css_selector):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"body > div.ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-dialog-buttons.ui-draggable.ui-resizable > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)"}
(Session info: chrome=81.0.4044.138)
Question: How to fix my code?
Upvotes: 0
Views: 256
Reputation: 153
The problem is the find element method throws an exception when the element is not found. You can catch the exception like this:
try:
ignore_btn_status = driver.find_element_by_css_selector("body > div.ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-dialog-buttons.ui-draggable.ui-resizable > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)")
ignore_btn_status.click()
except:
print("Button not found")
To be more specific you can catch the NoSuchElementException. For more information see here: https://docs.python.org/3/tutorial/errors.html
Upvotes: 2
Reputation: 312
Just put it inside a try except:
try:
ignore_btn_status = driver.find_element_by_css_selector("body > div.ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-dialog-buttons.ui-draggable.ui-resizable > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)")
ignore_btn_status.click()
except:
print("does not exist")
Also not that I removed the [0], because you did 'find_element' and not 'find_elements'
Upvotes: 1