我的脑子已经有一段时间了,似乎无法弄清楚..我希望点击我的雅虎帐户中的电子邮件。如果我指定我正在寻找的值,它可用于查找第一封电子邮件。我需要能够根据主题找到任何电子邮件。 如果它包含“POST / EDIT / DELETE:* 2007 NO DAMAGE TOYOTA CAMRY XLE”,则可以点击第一个主题
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'POST/EDIT/DELETE: *2007 NO DAMAGE TOYOTA CAMRY XLE')]"))).click();
另一方面,如果我将其变为变量,则不会:
String emailtitle = "POST/EDIT/DELETE: "+ "*2007 NO DAMAGE TOYOTA CAMRY XLE";//Build email string
System.out.println("Email Title is: "+ emailtitle);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'" + emailtitle + "')]"))).click();
我正在搜索的地方:
<div class="subj" role="gridcell" id="yui_3_16_0_ym19_1_1509685997190_1725"> <span dir="" class="subject " title="POST/EDIT/DELETE: *2007 NO DAMAGE TOYOTA CAMRY XLE ( cars & trucks - by owner)" id="yui_3_16_0_ym19_1_1509685997190_1724"> POST/EDIT/DELETE: *2007 NO DAMAGE TOYOTA CAMRY XLE ( cars & trucks - by owner) </span> <span class="thread-snippet" role="gridcell" dir="" id="yui_3_16_0_ym19_1_1509685997190_1754"> IMPORTANT - FURTHER ACTION IS REQUIRED TO COMPLETE YOUR REQUEST !!! FOLLOW THE WEB ADDRESS BELOW TO: PUBLISH YOUR AD EDIT (OR CONFIRM AN EDIT TO) YOUR AD VERIFY YOUR EMAIL ADDRESS DELETE YOUR AD If not clickable, please copy and paste the address to your browser: THIS LINK IS A PASSWORD. DO NOT SHARE IT - anyone who has a copy of this link can edit or delete your posting. link PLEASE KEEP THIS EMAIL - you may need it to manage your posting! Your posting </span> </div>
理想情况下,我希望能够根据电子邮件的标题更改电子邮件标题。所以使用变量是必须的。但由于某些原因,当我将它用作变量时,它不会返回值。这会发生什么原因吗?
答案 0 :(得分:0)
尽量将String
emailtitle
简单化为:
String emailtitle = "POST/EDIT/DELETE: *2007 NO DAMAGE TOYOTA CAMRY XLE";
而不是:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'" + emailtitle + "')]"))).click();
您可以尝试:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[.='" + emailtitle + "']")));
wait
将返回该元素。因此,请在WebElement
的帮助下,在下一步中调用click()
。
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[.='" + emailtitle + "']")));
elem.click();
答案 1 :(得分:0)
尝试一次
String emailtitle = "POST/EDIT/DELETE: "+ "*2007 NO DAMAGE TOYOTA CAMRY XLE";//Build email string
System.out.println("Email Title is: "+ emailtitle);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text(),emailtitle]")));
driver.findElement(By.xpath("//*[text(),emailtitle]")).click();
如果xpath返回多个元素,则尝试根据
之类的索引搜索特定元素...By.xpath("
(//*[text(),emailtitle]")[1]).click()//try with different like 0,1,2
正如你在下面的图片中看到的,我的xpath返回了7个匹配,所以通过索引我试图获取我的特定元素,然后在使用新的xpath后我可以点击元素
答案 2 :(得分:0)
最好避免使用字符串连接构造XPath表达式。最好的情况是,由于你在每次执行表达式时重新编译表达式,因此引入与引号混淆和性能问题,你可能会遇到像这样的调试麻烦。在最坏的情况下,您的代码对注入攻击持开放态度。
我不太确定你问题中的标签是什么XPath API,但是大多数XPath API允许你编译包含变量引用的表达式:
x = xpath.compile("//something[contains(@att, $value)]");
然后在执行时为参数提供一个值,如:
y = x.load();
y.setParam("value", theValueIAmLookingFor)
y.evaluate();