我不知道如何使用 Selenium 和 vba 更改浏览器选项卡顶部显示的标题或名称。浏览器是 Edge chromium。以 stack overflow 站点为例...如果我想将选项卡顶部的文本从“Stack Overflow”更改为“我最喜欢的网站!”,我可以进入 devtools 控制台并输入 document。 title = "我最喜欢的网站!"
我通过 excel ba 和 selenium 做了一些自动化,并且有几个标签都具有相同的标题。当我启动我的脚本并打开选项卡时,我希望能够给每个选项卡一个唯一的名称。
我在研究时发现的一切都是关于获取当前显示的标题或如何循环浏览选项卡以找到您想要的那个。除了 devtools 中的方法或通过实际更改显示内容的浏览器扩展之外,没有发现任何东西。
这里是一些示例代码...
Set D = New EdgeDriver
With D
.Start "Edge"
.Window.SetSize 1130, 960
.Window.SetPosition 400, 0
.Get HubURL
.FindElementByCss("a:nth-child(4)").Click 'KINSTON PAGE
.Wait (2000)
Do While .Url <> KinstonURL
.SwitchToNextWindow
Loop
.Wait (1000)
.FindElementByXPath("/html/body/div/div/div/_div/div/div/ _
div/div/div[2]/h3/span[1]", timeout:=30000).Click 'SHOW ALL
.Wait (2000)
.FindElementByXPath("//*[@id='itemsPanel']/div[2]/menu- _
.item[1]item[1]/div/img", timeout:=20000).Click 'ALERT INBOX
'***===Here is the line I added per the first answer below===***
D.ExecuteScript ("document.title = Kinston MES Inbox")
End With
Here is the runtime error it gives me:
[1]: https://i.stack.imgur.com/KgqOy.jpg
解决方案非常简单:告诉 WebDriver 执行 JavaScript。
driver.ExecuteScript ("document.title = 'My favorite website ever!'")
天哪,蝙蝠侠,它成功了!谢谢。我以前从未使用过 executescript。在阅读这篇文章时,我记得看到过一些关于以同样的方式使用 javascript 与可能不可见的元素进行交互的内容。当尝试运行 headless edge 或 chrome 时,id 总是有一个或两个元素会阻止脚本说元素不可见。这在那些情况下也能起作用吗?