css - 如何获取包裹在 <p> 标签下的段落中的文本的 CSS 选择器

标签 css xpath selenium-webdriver

我正在尝试使用 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/

相关文章:

jquery - 在 jQuery 更改了 div 中元素的尺寸后,如何获取 div 的高度?

xml - android中的xpath评估错误

selenium-webdriver - 如何防止 FirefoxDriver 的日志记录?

javascript - python从skyscanner抓取航类和价格数据

selenium-webdriver - 如何使用/附加使用 Selenium 的现有浏览器?

html - CSS 链接标签有效但@import 无效

css - CSS3的:root pseudo class and html?有什么区别

css - 背景图片一张张,怎么样?

xml - 获取不同层中的所有 <PRO>-标签

xml - 如何根据内容选择 XML 节点?