关于Selenium WebDriver:sendKeys(Keys.DOWN)不会移至自动提示下拉列表中的选项

sendKeys(Keys.DOWN) is not moving to the option in the auto suggestive dropdown

Ocupation我正在尝试从自动提示下拉列表中选择一个选项。 当我在下拉列表中输入字符串时,sendKeys(keys.down)不会移至该选项。 我收到"无法关注元素"错误。
我已经添加了co Aviva Code de的屏幕截图和屏幕上的下拉菜单。 这是代码。

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();
字符串脚本="返回document.getElementById('proposer.occupation-selectized')。value;";

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
        WebElement Occupation_textbox = driver.findElement(By.id("proposer.occupation-selectized")); //To locate the occupation textbox
    Occupation_textbox.sendKeys("Develop"+Keys.DOWN+"
"); //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+"
")";

    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);