运行Selenium脚本在servicenow上进行一些自动化测试 尝试填充网页上的字段时出现找不到元素的错误。登录页面具有iframe。但是登录后,我认为下一页没有iframe。还尝试过driver.switch_to.default_content(),但这似乎没有帮助。 我知道该元素在那里并且具有该ID,因为我看过html。还尝试填充其他两个字段,但存在相同的问题。 有什么建议么?谢谢。
它最初尝试访问的URL是-https://dev85222.service-now.com/incident.do,但在此之前,浏览器转到登录页面,即 https://dev85222.service-now.com/navpage.do,然后登录后,您将直接转到event.do。脚本获取第二个URL后,将产生错误-找不到元素 我认为可能与切换iframe有关
代码-
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://dev85222.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("admin")
password = driver.find_element_by_id("user_password")
password.send_keys("Liverpool28")
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")
错误-找不到元素 Ë ================================================== ==================== 错误:test_selenium(主要 .IncidentCreate) -------------------------------------------------- -------------------- 追溯(最近一次通话): test_selenium中的文件“ C:/用户/用户/文档/自动化/FFOX_CLOUD_INC_CREATEv1.py”,行66 类别= driver.find_element_by_id(“ incident.category”) 在find_element_by_id的第359行中,文件“ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py” 返回self.find_element(by = By.ID,value = id_) 文件“ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,行966,在find_element中 'value':value})['value'] 执行中的文件“ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py”,执行 self.error_handler.check_response(响应) 文件“ C:\ Users \ user \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py”,第242行,在check_response中 引发exception_class(消息,屏幕,堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:[id =“ incident.category”]
----------------------------------------------------------------------
Ran 1 test in 41.024s
答案 0 :(得分:0)
切换到默认内容,在单击Enter即可从第一页登录。这解决了问题-driver.switch_to.default_content()