Search code examples
javascriptseleniumselenium-webdriverlinktextpartiallinktext

TypeError: Invalid locator error using By.linkText containing dot characters (Selenium)


I've looked around on other SO questions, but I don't see anything that could help me. My error, as the title suggests, is the TypeError: Invalid locator error. Here is part of my code (where I think the problem occurs), so that you can see what is wrong with it:

(async function(){
            let driver = await new Builder().forBrowser('chrome').build();
            try 
            {
                await driver.get(stockLink);
                if (driver.findElement(By.linkText("View at Amazon.co.uk") === true))
                {
                    let amazonLink = await driver.wait(until.elementLocated(By.linkText('View at Amazon.co.uk')), 5000);
                    await amazonLink.click();
                }

            }
            finally
            {
                await driver.quit();
            }
            
        })();        

All variables used in this portion of code have been assigned a value.

Edit:

I'll explain what the code is supposed to do, if it helps:

The code clicks on a link, which leads to another link on Amazon. I can get the first link to open, but after the program attempts to open the second link, I get the TypeError: Invalid locator error.

Edit 2:

Here is the stack trace:

Trace
    at Client.<anonymous> (C:\Users\user\project\index.js:55:17)
    at Client.emit (node:events:378:20)
    at MessageCreateAction.handle (C:\Users\user\project\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)     
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\user\project\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\user\project\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\user\project\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)    
    at WebSocketShard.onMessage (C:\Users\user\project\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)   
    at WebSocket.onMessage (C:\Users\user\project\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (node:events:378:20)
    at Receiver.receiverOnMessage (C:\Users\user\project\node_modules\ws\lib\websocket.js:825:20)
at C:\Users\user\project\index.js:75:25
    at processTicksAndRejections (node:internal/process/task_queues:94:5)

Edit 3: Here is the error message:

TypeError: Invalid locator
    at Object.check [as checkedLocator] (C:\Users\user\project\node_modules\selenium-webdriver\lib\by.js:405:9)
    at Driver.findElement (C:\Users\user\project\node_modules\selenium-webdriver\lib\webdriver.js:971:18)
    at C:\Users\user\project\index.js:66:28
    at processTicksAndRejections (node:internal/process/task_queues:94:5)

Solution

  • Within Locator Strategies . is interpreted differently, maorly to denote classnames. Hence the error.


    Solution

    You can change the Class By from:

    By.linkText('View at Amazon.co.uk')
    

    To:

    By.partialLinkText('View at Amazon')
    

    Update

    Effectively your line of code will be:

    if (await driver.findElement(By.linkText("View at Amazon.co.uk")) == true))