Xpath-I need to use a variable in XPATH

Viewed 710

I am using Selenium-webdriver and I need to select an element by it's "text". So, I am using xpath. But, the value will vary, so I use a variable. Please tell me how to fix this, or do a better way of doing it.

Code:

const { Builder, By, Key, until } = require('selenium-webdriver');
let category = null;
category = "jackets";
async function main() {
    let driver = new Builder().forBrowser('chrome').build();

    driver.get('https://supremenewyork.com');

    driver.findElement(By.className('shop_link')).click();
    let element0 = await driver.wait(until.elementLocated(By.xpath("//*[contains(@href,'http://www.supremenewyork.com/shop/all')]", 10000)));
    await element0.click();
    //bugged code start below
    let element1 = await driver.wait(until.elementLocated(By.xpath("//*[contains(text(),"+category+")]", 10000)));
    await element1.click();
    //bugged code end
}

main();

The error is:

WebDriverError: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
1 Answers

You are missing quotes around the text value in the XPath expression:

let element1 = await driver.wait(until.elementLocated(By.xpath("//*[contains(text(),'"+category+"')]", 10000)));                                                                               
Related