Running a selenium script to do some automated testing on servicenow Getting an element not found error when trying to populate a field on the webpage. The login page has an iframe. But after login, the next page I dont think has an iframe. Also tried driver.switch_to.default_content() but this didnt seem to help. I know the element is there and has that ID because ive looked at the html. Also tried populating a couple of other fields but had the same issue. Any suggestions? Thanks.
Originally the url it tries to go to is - https://dev85222.service-now.com/incident.do, but before that the browser goes to the login page which is https://dev85222.service-now.com/navpage.do, then after logging in, you get directed to incident.do. Once the script gets the the second url it produces the error -Element not found I think it might be to do with switching iframes
The code -
from selenium import webdriver
import time
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import date
from selenium.webdriver.common.action_chains import ActionChains
class IncidentCreate(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_selenium(self):
#
identifier = "AUTOMATED TESTING"
module = "INCIDENT"
action = "CREATE"
job_name = ""
today = str(date.today())
driver = self.driver
base_url = "https://dev85999882.service-now.com/incident.do"
driver.get(base_url)
driver.implicitly_wait(5)
driver.switch_to.frame("gsft_main")
username = driver.find_element_by_id("user_name")
username.send_keys("username")
password = driver.find_element_by_id("user_password")
password.send_keys("password")
password.send_keys(Keys.RETURN)
identifier_inc = ("AUTOMATED TESTING - INCIDENT - %s" %today)
driver.switch_to.default_content()
time.sleep (10)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "incident.category"))
)
except:
print("Element not found")
The error - Element not found E ====================================================================== ERROR: test_selenium (main.IncidentCreate) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/user/Documents/Automation/FFOX_CLOUD_INC_CREATEv1.py", line 66, in test_selenium category = driver.find_element_by_id("incident.category") File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element 'value': value})['value'] File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute self.error_handler.check_response(response) File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="incident.category"]
----------------------------------------------------------------------
Ran 1 test in 41.024s