Selenium 边缘浏览器中的选择器异常无效,而在chrome浏览器中可以毫无异常地找到相同的元素
我正在尝试单击一个元素。当我在chrome浏览器上运行测试脚本时,元素被单击,当我在edge浏览器上运行相同的测试脚本时,出现异常“InvalidSelectorException” 铬元素的图像-[ 在边缘中找到的元素图像-[ 代码中使用的Xpath:Selenium 边缘浏览器中的选择器异常无效,而在chrome浏览器中可以毫无异常地找到相同的元素,selenium,selenium-webdriver,xpath,selenium-chromedriver,microsoft-edge,Selenium,Selenium Webdriver,Xpath,Selenium Chromedriver,Microsoft Edge,我正在尝试单击一个元素。当我在chrome浏览器上运行测试脚本时,元素被单击,当我在edge浏览器上运行相同的测试脚本时,出现异常“InvalidSelectorException” 铬元素的图像-[ 在边缘中找到的元素图像-[ 代码中使用的Xpath: driver.findElement(By.xpath("//a[contains(text(),'Dashboard')]")) 我们可以看到,edge browser中的“dashboard”元素文本中没有引号,而chrome中的元素文本
driver.findElement(By.xpath("//a[contains(text(),'Dashboard')]"))
我们可以看到,edge browser中的“dashboard”元素文本中没有引号,而chrome中的元素文本中也有引号
这就是在edge中作为无效选择器的原因吗
Dashboard.java:173 - Exception occured :
org.openqa.selenium.InvalidSelectorException: The specified selector is
invalid.
For documentation on this error, please visit:
https://www.seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-
14T08:17:03'
System info: host: 'IND-L-SRAWAT', ip: '164.99.196.169', os.name:
'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version:
'1.8.0_201'
Driver info: org.openqa.selenium.edge.EdgeDriver
Capabilities {acceptInsecureCerts: false, browserName: MicrosoftEdge,
browserVersion: 44.17763.1.0, javascriptEnabled: true, pageLoadStrategy:
normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), s
etWindowRect: false, timeouts: {implicit: 0, pageLoad: 300000, script:
30000}}
Session ID: 4BC8634F-5CCA-4747-BE2F-B7E4A0D59872
*** Element info: {Using=xpath, value=//a[contains(text(),'Dashboard')]}
2019-08-19 17:02:44 INFO SettingsUIOperationsTest.java:104 - Test Case
Failed
有人能帮我做点什么吗
****-编辑****
我知道了
有人能解释一下吗?不要使用
contains(text(),'Dashboard')
尝试使用以下任一解决方案:
- 使用
:text()
- 使用
:contains()
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<a href="#">Dashboard</a>
</body>
</html>
两个浏览器的输出:
我建议您尝试安装Windows操作系统的最新更新以更新边缘浏览器,然后再次尝试使用我的示例代码进行测试,以检查是否解决了该问题
如果问题仍然存在,请尝试提供详细代码以产生问题。我们将再次尝试测试它。我在过去几天才开始使用Selenium Web驱动程序,但发现By.XPath方法有一些非常奇怪的行为 这就是我使用的XPath(虽然元素100%存在,但它找不到该元素) 在尝试通过XPath加载一堆不同的语法后(失败了),我决定返回Selenium IDE并将任务导出为Java(我是C#),以查看发生了什么 事实证明,导出的Java测试将XPath中的单引号转义为如下内容
//a[contains(@href, \'/Web/Login\')])[2]
因此,只要我修改选择器以避开单引号,它就工作得很好
让我知道它是否有效
- 硒生成试验
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<a href="#">Dashboard</a>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new EdgeDriver();
//IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(@"C:\Users\panchals\Desktop\tests\215.html");
Console.WriteLine(driver.Title.ToString());
object element=driver.FindElement(By.XPath("//a[contains(text(),'Dashboard')]"));
Console.WriteLine(element.ToString());
driver.Close();
}
}
}
//a[contains(@href, '/Web/Login')])[2]
//a[contains(@href, \'/Web/Login\')])[2]