【问题标题】:Error while using Drop-down - Python Selenium使用下拉菜单时出错 - Python Selenium
【发布时间】:2020-07-30 15:32:45
【问题描述】:

大家好

我正在尝试使用 class_name/xpath 从下拉列表中选择一个值 但它不起作用。尝试使用 id 但发现 id 保留 变化。在这方面需要帮助。

This is the dropdown where im trying to select short description

下面是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
import time

username = '*****'
password = '*****'

url = 'https://*****.service-now.com/'

driver = webdriver.Chrome("C:\WebDrivers\chromedriver.exe")
driver.get(url)

driver.switch_to.frame('gsft_main')
driver.maximize_window()

driver.find_element_by_id('user_name').send_keys(username)
driver.find_element_by_id('user_password').send_keys(password)
driver.find_element_by_id('sysverb_login').click()

wait = WebDriverWait(driver, 10)
incident = wait.until(EC.visibility_of_element_located((By.XPATH, '//span[contains(text(), 
"Incident")]')))
incident.click()

open = driver.find_element_by_link_text('Open - Unassigned')
open.click()

time.sleep(25)

element = driver.find_element_by_class_name('form-control default-focus-outline')
dropdown.Select(element)
dropdown.select_by_value('short_description')

以下是我复制并获得的元素。 复制元素:

<select id="d5f43fd407945410af12f2ae7c1ed05c_select" class="form-control default-focus-outline" aria- 
expanded="false"><option value="zztextsearchyy" selected="SELECTED" role="option">for text</option> 
<option value="number" role="option">Number</option><option value="opened_at" 
role="option">Opened</option><option value="short_description" role="option">Short 
description</option><option value="caller_id" role="option">Caller</option><option value="priority" 
role="option">Priority</option><option value="state" role="option">State</option><option 
value="category" role="option">Category</option><option value="assignment_group" 
role="option">Assignment group</option><option value="assigned_to" role="option">Assigned to</option> 
<option value="sys_updated_on" role="option">Updated</option><option value="sys_updated_by" 
role="option">Updated by</option></select>

通过 xpath:

//*[@id="d5f43fd407945410af12f2ae7c1ed05c_select"]

给我以下错误:

DevTools listening on ws://127.0.0.1:49915/devtools/browser/30b98d3d-0779-4d85-86bc-9af3a24726a2
[7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
  File "Test.py", line 39, in <module>
    element = driver.find_element_by_class_name('form-control default-focus-outline')
  File "C:\Python38\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:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in 
find_element
return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
  File "C:\Python38\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":".form-control default-focus-outline"}
  (Session info: chrome=81.0.4044.113)

上面的消息有两个不同的错误:

1) [7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)] XXX 初始化()

2) 无法定位 元素:{"method":"css selector","selector":".form-control default-focus-outline"}

需要有关如何解决这些问题的帮助。

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver automation servicenow


    【解决方案1】:

    尝试使用 WebDriverWait 解决您的问题。如果您仍然遇到问题,请检查您的元素是否存在于 iframe 中

    wait = WebDriverWait(driver, 30)
    wait.until(EC.element_to_be_clickable((By.XPATH, "//select[@id='form-control default-focus-outline']/option[text()='Short description']")))
    

    注意:请在您的解决方案中添加以下导入

    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 import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import Select
    
    
    chrome_options = webdriver.ChromeOptions()
    options = Options()
    chrome_options.add_argument("--disable-notifications")
    
    
    
    driver = webdriver.Chrome(executable_path="C:\New folder\chromedriver.exe",chrome_options=chrome_options)
    username = '********'
    password = '********'
    
    
    url = 'https://*******.service-now.com/'
    driver.get(url)
    driver.maximize_window()
    wait = WebDriverWait(driver, 20)
    iframe = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'gsft_main')))
    driver.switch_to.frame(iframe)
    wait.until(EC.presence_of_element_located((By.ID, "user_name"))).send_keys(username)
    wait.until(EC.presence_of_element_located((By.ID, "user_password"))).send_keys(password)
    wait.until(EC.presence_of_element_located((By.ID, "sysverb_login"))).click()
    driver.switch_to.default_content()
    
    wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='filter']"))).send_keys("Incident")
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='sn-widget-list-title ng-binding'][contains(text(),'Open - Unassigned')]"))).click()
    
    iframe = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'gsft_main')))
    driver.switch_to.frame(iframe)
    print wait.until(EC.element_to_be_clickable((By.XPATH, "//h2[contains(@class,'navbar-title list_title')]"))).text
    
    element=wait.until(EC.element_to_be_clickable((By.XPATH, "//span[@class='input-group-addon input-group-select']//select")))
    element=wait.until(EC.presence_of_element_located((By.XPATH, "//span[@class='input-group-addon input-group-select']//select")))
    all_options = element.find_elements_by_tag_name("option")
    for option in all_options:
        print("Value is: %s" % option.get_attribute("value"))
        option.click()
    select = Select(element)
    select.select_by_visible_text("Short description")
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2020-02-22
    • 2019-04-05
    相关资源
    最近更新 更多