我正在尝试从自动提示下拉列表中选择一个选项。当我在下拉列表中输入字符串时,sendKeys(keys.down)不会移动到该选项。我收到了'无法聚焦元素'的错误。 我添加了co de的屏幕截图和屏幕上的下拉列表。这是代码。
JavascriptExecutor jse =(JavascriptExecutor)驱动程序; jse.executeScript(“document.getElementById('proposer.occupation-selectized')。value ='Composer';”); driver.findElement(By.xpath(“// * [@ id中= '内容'] /格4 / DIV / DIV 1 /格[8] / DIV 2 /格/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);
答案 0 :(得分:0)
您可以使用以下代码从keys.down
中选择autosuggestion下拉列表中的选项 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);