【问题标题】:How do we select the auto hideded value option first and then select the required options?我们如何先选择自动隐藏值选项,然后再选择需要的选项?
【发布时间】:2019-12-30 07:16:55
【问题描述】:

我们如何先选择自动隐藏值选项,然后再选择需要的选项? 我的 HTML 文件如下:

在选择域之前:

div class="col-xs-12">
                    <label class=" required-field" for="Domain">Domain</label>
                    <select autocomplete="off" class="form-control select2-hidden-accessible valid" data-val="true" data-val-required="The 'Domain' field is required." id="Domain" name="Domain" tabindex="-1" aria-hidden="true" aria-describedby="Domain-error" aria-invalid="false">
                    <option value="">Select a domain...</option>
</select><span class="select2 select2-container select2-container--bootstrap select2-container--below select2-container--focus" dir="ltr" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-Domain-container"><span class="select2-selection__rendered" id="select2-Domain-container" title="Select a domain...">Select a domain...</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
                    <span class="field-validation-valid text-danger" data-valmsg-for="Domain" data-valmsg-replace="true"></span>
                </div>

域名选择后:

<select autocomplete="off" class="form-control select2-hidden-accessible" data-val="true" data-val-required="The 'Domain' field is required." id="Domain" name="Domain" tabindex="-1" aria-hidden="true"><option value="">Select a domain...</option>
<option value="ABC.abc.com">ABC.abc.com</option></select>
<span class="select2 select2-container select2-container--bootstrap select2-container--below select2-container--focus" dir="ltr" style="width: 100%;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-Domain-container"><span class="select2-selection__rendered" id="select2-Domain-container" title="ABC.abc.com">ABC.abc.com</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>

当我们启动网站时,它将显示域名下拉列表和“选择域”选项值。如果我选择“选择域”选项,将显示实际域名。(意味着在我选择选择域选项之前,不会显示有效的域名列表)

我是 selenium 新手,所以我尝试使用 find_element_by_id 和可见文本选项选择选项

我尝试了下面的 python 代码。但它不起作用。

select_element = Select(driver.find_element_by_id('Domain'));
print ([o.text for o in select_element.options])
select_element.select_by_visible_text('Select a domain...');
select_element1 = Select(driver.find_element_by_id('Select a domain...'));
select_element.select_by_value('XYZ.xyz.com')
wait = WebDriverWait(driver, 10 )
print('success')

我需要选择如下顺序:

  1. 选择域按钮。
  2. 接下来我需要选择自动隐藏文本“选择域...”选项值。然后将显示域值。 (注意:Utill Select a Domain... 选项实际域名未列出) (有时即使我选择选择域自动隐藏域列表不立即列出的另一个重要点 可能需要等待一段时间)

  3. 加载域列表的等待时间

  4. 在那选择(下拉列表)“ABC.abc.com”域值
  5. 输入登录用户名
  6. 输入密码
  7. 连接页面。

如果有人共享 Python 的完整代码,则被占用

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    在选择 Select a domain... 前后,select 元素中的 class 名称发生了变化

    之前:

    class="form-control select2-hidden-accessible valid"
    

    之后:

    class="form-control select2-hidden-accessible"
    

    在元素 visibility_of_element_located 之前,您似乎需要 WebDriverWait

    导入这个:

    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.ui import WebDriverWait
    

    那就试试吧:

    select_element = Select(driver.find_element_by_id('Domain'));
    print ([o.text for o in select_element.options])
    select_element.select_by_visible_text('Select a domain...');
    WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="Domain" and @class="form-control select2-hidden-accessible"]')))
    slct = Select(driver.find_element_by_xpath('//*[@id="Domain" and @class="form-control select2-hidden-accessible"]'));
    
    #update here
    driver.find_element_by_xpath('//*[@id="Domain" and @class="form-control select2-hidden-accessible"]').click()
    time.sleep(1)
    
    slct.select_by_visible_text('XYZ.xyz.com')
    #slct.select_by_value('XYZ.xyz.com')
    print('success')
    

    【讨论】:

    • 嗨,我仍然无法登录。 driver.implicitly_wait(10) select_element = Select(driver.find_element_by_id('Domain')); print ([o.text for o in select_element.options]) select_element = Select(driver.find_element_by_id('Select a domain...')); select_element.select_by_value('ABC.abc.com') print('success').a = driver.find_element_by_id('Username') a.send_keys('username) Error:File "C:\Python374\lib\site-packages \selenium\webdriver\remote\webdriver.py",第 360 行,在 find_element_by_id 中返回 self.find_element(by=By.ID, value=id_)
    • 更新了选域前后的html代码
    • 已更新答案,请重试。
    • 仍然是我无法选择的域名可能是这可能是不确定的原因 手动更新后
    • 我使用 print statement(print ([o.text for o in slct.options]) 在 slct.select_by_visible_text('XYZ.xyz.com') 行之前检查。仍然在打印 ['Select仅域...']。我得到的错误如下 selenium.common.exceptions.NoSuchElementException:消息:无法找到具有可见文本的元素:XYZ.xyz.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2011-03-11
    • 2013-01-13
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多