简体   繁体   中英

selenium-webdriver - JavaScript, findElement a href, <> is not a function

On the website https://mom.maison-objet.com/fr/marque/10008/rory-dobner-ltd?request_type=meeting#xtor=AL-1459 , I'm trying to get the website url of the brand information which I can do in the console in JavaScript:

document.getElementById('phone_number').getElementsByTagName('a')[1].getAttribute('href')

But with the selenium-webdriver in JavaScript, I can't seem to find it. Here is my code:

let swd = require("selenium-webdriver");
let browser = new swd.Builder();
let tab = browser.setChromeOptions().forBrowser("chrome").build();

async function main() {

    await tab.manage().setTimeouts({ implicit: 10000 })

    await tab.get("https://mom.maison-objet.com/fr/marque/10008/rory-dobner-ltd?request_type=meeting#xtor=AL-1459");

    await tab.sleep(3000);

    let website = await tab.findElement(swd.By.css('#phone_number a:nth-child(2)').getAttribute('href'))
    console.log(website)

}

main()

I'm getting this error:

(node:24520) UnhandledPromiseRejectionWarning: TypeError: swd.By.css(...).getAttribute is not a function

I tried also:

await tab.findElement(swd.By.css('#phone_number a:nth-of-type(2)').getAttribute('href'))

I try to simplify my code with:

let website = await tab.findElement(swd.By.css('#phone_number').getText())

But I get the same error and I don't know how to solve it.

This was the issue: calling

.getAttribute('href')

inside the method findElement, but it should had been outside:

await tab.findElement(swd.By.css('#phone_number a:nth-of-type(2)')).getAttribute('href')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM