0

I'm using nodeJS, this is what I'm trying:

for (let i = 1; i <= elSize; i++) {
  try {
    let DeviceName = await driver
      .findElement(By.xpath("//span[@class='a-size-medium a-color-base a-text-normal']['i']"))
      .getText();
    console.log(i + ". Device Name: " + DeviceName + "\n");
  } catch (e) {
    await driver.executeScript(
      ...
      catch statements...
    );
  }
}

trying to insert 'i' of for loop variable in xpath. Couldn't add a double quote, xpath becomes unidentifiable for eg this:

driver.findElement(By.xpath("(//span[@class='a-size-medium a-color-base a-text-normal'])["+ i +"]")).getText();

Does not get identified.

This is how the xpath is discoverable in browser:

//span[@class='a-size-medium a-color-base a-text-normal']['i']

4
  • 1
    Use template literals (backticks `) Commented Sep 9, 2022 at 8:38
  • 1
    try to use template quotes instead of double quotes to keep things simple: `//span[@class='a-size-medium a-color-base a-text-normal']['${i}']"` Commented Sep 9, 2022 at 8:38
  • 1
    A much more readable alternative is jsdom and querySelectorAll(".a-size-medium.a-color-base.a-text-normal")[i] Commented Sep 9, 2022 at 8:42
  • Thank you for the suggestions usage of template literals worked. I also followed this (//someName)[n] to point the nth occurrence. This is the modified line: let DeviceName = await driver.findElement(By.xpath(`(//span[@class='a-size-medium a-color-base a-text-normal'])[${i}]`)).getText(); Commented Sep 9, 2022 at 9:09

2 Answers 2

1

You can use a String.format to format the XPath expression with the i index value, something like this:

var locator = "(//span[@class='a-size-medium a-color-base a-text-normal'])[{0}]"
for (let i = 1; i <= elSize; i++) {
  var localLocator = String.format(locator, i);
  try {
    let DeviceName = await driver.findElement(By.xpath(localLocator)).getText();
    console.log(i + ". Device Name: " + DeviceName + "\n");
  } catch (e) {
    await driver.executeScript(
      ...
      catch statements...
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Building and compiling a new XPath expression for each value of i is going to be very inefficient, although it's quite possible. I'm not familiar with the XPath API you are using, but many XPath APIs allow you to compile an XPath expression containing a variable reference, for example

//span[@class='a-size-medium a-color-base a-text-normal'][$i]

and then to execute it repeatedly with different values of the parameter.

Alternatively, you can easily retrieve all the items in one go

//span[@class='a-size-medium a-color-base a-text-normal']

and then iterate over them in the host language (Javascript in this case).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.