我正在尝试使用 Xpath 选择器(Xpath:html/body/p[2]/text()[3])捕获邮件段落中的 URL。我无法获得正确的 css 选择器,但使用 Xpath,在执行 Selenium webdriver 测试时出现如下错误。
<body>
<p>Dear EmailTest user,</p>
<p>
Your profile on the Swwebsite has been successfully created.
<br/>
To begin, click on the link below. You will be prompted to create a password during the login process.
<br/>
https://test.website.com/one/portal/$swweb/?uri=emailuser-125633uu3d-452iekdkd
<br/>
After creating your password, you will be able to access your application with the below username:
<br/>
User Name emailuser_test
<br/>
</p>
出现以下错误:
com.ibm.automation.wtf.driver.DriverException: TypeError: Argument 1 of Window.getComputedStyle does not implement interface Element.
Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
在互联网上的一些搜索中,它似乎是 Firefox 的 CSS 问题。有人可以帮我解决这个问题或获得一个合适的 CSS 选择器来捕获邮件中的 URL
System info: host: 'IBM345-R902EWZ3', ip: '9.162.252.164', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
最佳答案
首先获取<p>
下的整个邮件正文使用 getText()
标记方法,然后使用 split/substring/indexOf 方法等 Java String 操作捕获 URL。
像下面这样使用 cssSelector:
String mailBody = driver.findElement(By.cssSelector("body > p:nth-child(2)")).getText();
现在解析字符串以捕获 URL。
例如:
String[] temp = mailBody.split("\n");
String url = temp[2];
System.out.println(url);
或者
int startIndex = mailBody.indexOf("https");
int endIndex = mailBody.indexOf("After creating your password");
String url = mailBody.substring(startIndex, endIndex - 1);
System.out.println(url);
或者您可以使用任何字符串操作(或正则表达式)从 mailBody
中搜索 URL字符串
另一个尝试:
您还可以使用 xpath html/body/p[2]
获取整个邮件正文。然后解析字符串。
关于css - 如何获取包裹在 <p> 标签下的段落中的文本的 CSS 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639048/