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

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

我查看了其他 SO 问题,但没有看到任何可以帮助我的东西。如标题所示,我的错误是 TypeError: Invalid locator 错误。这是我的代码的一部分(我认为问题出在哪里),以便您可以看到它有什么问题:

(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();
            }
            
        })();        

这部分代码中使用的所有变量都已赋值。

编辑:

我会解释代码应该做什么,如果有帮助的话:

代码点击了一个 link,这导致了亚马逊上的另一个 link。我可以打开第一个 link,但在程序尝试打开第二个 link 后,我收到 TypeError: Invalid locator 错误。

编辑 2:

这是堆栈跟踪:

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)

编辑 3:这是错误消息:

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)

. 中有不同的解释,主要表示 classnames。因此错误。


解决方案

您可以将 Class By 更改为:

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

收件人:

By.partialLinkText('View at Amazon')

更新

实际上你的代码行将是:

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