0
votes

OcupationI'm trying to select an option from an auto suggestive dropdown. When I enter the string into the dropdown sendKeys(keys.down) is not moving to the option. I'm getting a 'cannot focus element' error. I have added screen shots of the coAviva Codede and the dropdown on the screen. This is the code.

JavascriptExecutor jse = (JavascriptExecutor) driver; 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);
1
Please share (at least ) the code, and the full exception. Asking questions in the right format will increase your chances of a decent answer dramatically.Chai

1 Answers

0
votes

you can use the following code to select the options from the autosuggestion drop down using 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);