1

On a single page on our website, there are two elements that are.. very very similar:

"A"

<select class="form-control custom-select ng-untouched ng-pristine ng-invalid" id="SelectEU" name="SelectEU" required="">

"B"

<select class="form-control custom-select ng-untouched ng-pristine" id="SelectEU" name="SelectEU" required="" disabled="">

the differences are :

A has class ng-pristine

B has property disabled=""

How can I click on the first select, A, then select a particular value??

I have tried the following to no avail..

I tried selecting based on ng-pristine

element = await driver.findElement(By.xpath("//select[@class='form-control custom-select ng-untouched ng-pristine ng-invalid']"))[0];  

I tried selecting based on [0]..

element = await driver.findElement(By.xpath("//select[@id='SelectEU']"))[0];  

I tried selecting by the text (A contains the text "Electric Utility"

element = await driver.findElement(By.xpath("//option[@id='SelectEU' and text()='Electric Utility']"))[0];

It all has the error: no such element exists

7
  • You almost had it. The first match is [1] not [0]. element = await driver.findElement(By.xpath("//select[@id='SelectEU'][1]")); Jul 16, 2018 at 19:54
  • Alternatively you could use the original locator with the [0] outside of the path, but make the findElement plural: element = await driver.findElements(By.xpath("//option[@id='SelectEU' and text()='Electric Utility']"))[0]; Jul 16, 2018 at 19:56
  • Oh my.. I see my error. I mistyped element = await driver.findElement(By.xpath("//select[@id='SelectEU']"))[0]; it should have been element = await driver.findElement(By.xpath("//select[@id='SelectEU'][1]")); Very grateful. I would gladly accept your answer and upvote sir if you provide it here... Jul 16, 2018 at 20:24
  • I wasn't 100% positive that my solution would work, which is why I offered it as a comment instead of an answer. I appreciate your acknowledgement. Jul 16, 2018 at 20:38
  • @BillHileman on your second point in this thread, "alternatively you could use the original locator with the [0] outside..." How would you scroll to such an element? await driver.executeScript("arguments[0].scrollIntoView()", element); returns an error for me.. Jul 16, 2018 at 21:18

1 Answer 1

1

You almost had it. The first match is [1] not [0].

Change your line to:

element = await driver.findElement(By.xpath("//select[@id='SelectEU'][1]"));

Edit: Regarding your second question in your main post comments

I do not have the nodejs knowledge to provide you the exact syntax of scrolling an element into view, however this is the JavaScript routine I use in Java to accomplish that requirement:

/** 
 * Scroll a web element into view
 * @author Bill Hileman
 * @param ele - WebElement
 */
public void scrollIntoView(WebElement ele) {

    ((JavascriptExecutor)driver).executeScript("window.scrollTo(" + ele.getLocation().x + 
                                                                "," + ele.getLocation().y + ")");

}
1
  • added extra lines to the code @BillHileman, but now it works :) Jul 22, 2018 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.