0

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
anfield
  • 323
  • 3
  • 16
  • Tried the xpath also, same error – anfield May 07 '19 at 01:13
  • Check my answer in this post to [find the iframe which have the element](https://stackoverflow.com/questions/55536851/is-there-a-way-to-search-webelement-on-a-main-window-first-if-not-found-then-s/55537186#comment97778812_55537186) added the python implementation too. – supputuri May 07 '19 at 01:45
  • Thanks. Made progress, as soon as I hit return after entering the password, then switch to default content, seem to be able to access the fields on the next page now – anfield May 07 '19 at 02:13
  • Cool! Glad that your able to make progress. – supputuri May 07 '19 at 02:17

1 Answers1

0

switched to default content, right after clicking enter to login from the first page. This resolved the issue - driver.switch_to.default_content()

anfield
  • 323
  • 3
  • 16