【问题标题】:sendKeys(Keys.DOWN) is not moving to the option in the auto suggestive dropdownsendKeys(Keys.DOWN) 没有移动到自动提示下拉菜单中的选项
【发布时间】:2018-11-01 12:31:29
【问题描述】:

我正在尝试从自动提示下拉菜单中选择一个选项。当我将字符串输入下拉列表时,sendKeys(keys.down) 不会移动到该选项。我收到“无法聚焦元素”错误。 我添加了 code 的屏幕截图和屏幕上的下拉菜单。这是代码。

JavascriptExecutor jse = (JavascriptExecutor) 驱动程序; jse.executeScript("document.getElementById('proposer.occupation-selectized').value = 'Composer';"); driver.findElement(By.xpath("//*[@id='content']/div4/div/div1/div[8]/div2/div/div1/ div")).sendKeys(Keys.DOWN); driver.findElement(By.id("proposer.occupation-selectized")).getText(); String script = "return document.getElementById('proposer.occupation-selectized').value;";

    String text=(String) jse.executeScript(script);
    System.out.println(text);

【问题讨论】:

  • 请分享(至少)代码,以及完整的例外情况。以正确的形式提出问题将大大增加您获得体面答案的机会。

标签: selenium-webdriver


【解决方案1】:

您可以使用以下代码通过 keys.down 从自动建议下拉菜单中选择选项

        WebElement Occupation_textbox = driver.findElement(By.id("proposer.occupation-selectized")); //To locate the occupation textbox
    Occupation_textbox.sendKeys("Develop"+Keys.DOWN+"\n"); //To enter the occupation search string and then go one down and select the option --or-- you can add a wait for the dropdown element to be visible and then take the current element and then go down (Keys.down)   "Occupation_textbox.sendKeys(Keys.DOWN+"\n")"; 

    WebElement Occupation_selected_text = driver.findElement(By.xpath(".//*[@id='proposer.occupation-selectized']/preceding-sibling::div")); //Locate element to capture the text of the selected option

    //There are two ways you can take out the value on your page
    String value_way1 = Occupation_selected_text.getAttribute("data-value"); // to take the text from data-value attribute 
    String value_way2 = Occupation_selected_text.getText(); // to get the text in the search field

    System.out.println(value_way1);
    System.out.println(value_way2);

【讨论】:

  • 您好 Ravikar,感谢您的解决方案。这很有效,我可以在下拉列表中搜索一个选项并选择它。我的 Java 经验很少,我正在通过一些在线教育来学习。
  • 你好@Paul,我很高兴听到这个消息。你能否将我的建议标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多