使用Selenium与Dropdown进行“嵌套输入”类交互?

时间:2019-10-31 23:39:24

标签: python selenium selenium-webdriver webdriver webdriverwait

我正在尝试自动向FCC投诉。我无法与位于https://consumercomplaints.fcc.gov/hc/en-us/requests/new?ticket_form_id=39744的下拉框进行交互。元素是 电话问题 下拉框:

enter image description here

HTML是:

<div class="form-field string  required  request_custom_fields_22619354" >
  <label id="request_custom_fields_22619354_label" for="request_custom_fields_22619354">Phone Issues</label>
  <input type="hidden" name="request[custom_fields][22619354]" id="request_custom_fields_22619354" autocomplete="off" data-tagger="[{&quot;label&quot;:&quot;-&quot;,&quot;value&quot;:&quot;&quot;},{&quot;label&quot;:&quot;Unwanted Calls (including do not call and spoofing)&quot;,&quot;value&quot;:&quot;telemarketing_phone&quot;},{&quot;label&quot;:&quot;Junk Faxes&quot;,&quot;value&quot;:&quot;junk_faxes_phone&quot;},{&quot;label&quot;:&quot;Availability&quot;,&quot;value&quot;:&quot;availability_phone&quot;},{&quot;label&quot;:&quot;Billing&quot;,&quot;value&quot;:&quot;billing_phone&quot;},{&quot;label&quot;:&quot;Cramming (unauthorized charges on your phone bill)&quot;,&quot;value&quot;:&quot;cramming_phone&quot;},{&quot;label&quot;:&quot;Equipment&quot;,&quot;value&quot;:&quot;equipment_phone&quot;},{&quot;label&quot;:&quot;Interference&quot;,&quot;value&quot;:&quot;interference_phone&quot;},{&quot;label&quot;:&quot;Number Portability (keeping your number if you change providers)&quot;,&quot;value&quot;:&quot;number_portability_phone&quot;},{&quot;label&quot;:&quot;Privacy&quot;,&quot;value&quot;:&quot;privacy_phone&quot;},{&quot;label&quot;:&quot;Rural Call Completion&quot;,&quot;value&quot;:&quot;rural_call_completion_phone&quot;},{&quot;label&quot;:&quot;Slamming (change of your carrier without permission)&quot;,&quot;value&quot;:&quot;slamming_phone&quot;}]" aria-required="true" aria-describedby="request_custom_fields_22619354_hint" aria-labelledby="request_custom_fields_22619354_label" />

  <p id="request_custom_fields_22619354_hint">Please select the issue that best describes your complaint.</p>
</div>

我要选择索引1或文本不需要的呼叫(包括请勿呼叫和欺骗)。 (索引0是带短划线的文本。)

这是我的三种尝试,但它们正在产生异常:

dropdown_issue = driver.find_element_by_id("request_custom_fields_22619354")

# First try - ElementNotInteractableException
dropdown_issue.send_keys("Unwanted Calls");

# Second try - ElementNotInteractableException
dropdown_issue.send_keys(Keys.DOWN);
dropdown_issue.send_keys(Keys.DOWN);
dropdown_issue.send_keys(Keys.ENTER);

# Third try - 'WebElement' object has no attribute 'select_by_index'
dropdown_issue.select_by_index(1)

我想我需要转到“ nesty-input”,它带有向下箭头(但没有名称或ID)。我不确定如何访问嵌套输入。

如何为下拉菜单选择所需的索引?

2 个答案:

答案 0 :(得分:1)

更新

使用WebDriverWait确保元素可见,并使用Select处理下拉菜单。

您需要等待,直到此元素可见:

By.XPATH -> //label[text()="Phone Issues"]//following-sibling::a[@class="nesty-input ae-exclude"]

select element是:

By.ID -> ae-request_custom_fields_22619354

请尝试以下代码:

driver.get('https://consumercomplaints.fcc.gov/hc/en-us/requests/new?ticket_form_id=39744')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//label[text()="Phone Issues"]//following-sibling::a[@class="nesty-input ae-exclude"]')))
elmt = driver.find_element_by_id('ae-request_custom_fields_22619354')
select = Select(elmt)
select.select_by_index(1)

#select by other method
select.select_by_value('telemarketing_phone')
select.select_by_visible_text('Unwanted Calls (including do not call and spoofing)')

正在导入:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

答案 1 :(得分:0)

要在website内与电话问题相邻的下拉框中选择文本为 Unwanted Calls (including do not call and spoofing) 的元素,您需要诱导< visibility_of_element_located()的em> WebDriverWait ,您可以使用以下Locator Strategies

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    #options.add_argument("--headless")
    #options.add_experimental_option("excludeSwitches", ["enable-automation"])
    #options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://consumercomplaints.fcc.gov/hc/en-us/requests/new?ticket_form_id=39744")
    driver.execute_script("scroll(0, 400)")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Please select the issue that best describes your complaint.']//preceding::a[1]"))).click()
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[text()='Please select the issue that best describes your complaint.']//preceding::select[@class='ae-select' and starts-with(@id, 'ae-request_custom_fields')]//option[contains(., 'Unwanted Calls')]"))).click()
    
  • 浏览器快照:

Unwantedcalls