Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium Webdriver Onlink文本_Selenium_Selenium Webdriver - Fatal编程技术网

Selenium Webdriver Onlink文本

Selenium Webdriver Onlink文本,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我应该在webdriver中使用什么命令来确保它单击特定文本,即上个月的 driver.findElement(By.xpath(".//a[text()='Last Month']")).click(); 代码是 <div class="dt_padded_medium_div"> <a onclick="setLastMonth()" href="#">Last Month</a> 我曾经 driver.findElement(By.xpath("//

我应该在webdriver中使用什么命令来确保它单击特定文本,即上个月的

driver.findElement(By.xpath(".//a[text()='Last Month']")).click();
代码是

<div class="dt_padded_medium_div">
<a onclick="setLastMonth()" href="#">Last Month</a>
我曾经

driver.findElement(By.xpath("//*[@id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]")).click();
但是仍然没有工作,我是不是错过了什么

更新:


代码正常运行,谢谢大家的帮助

如果页面中只有一个包含此文本的“a”元素,请尝试以下XPath:

//a[text()='Last Month']

如果有多个元素,请发布完整的HTML树,否则我们无法在不知道标记路径的情况下编写xpath,并确保它可以工作。首先,确保xpath工作正常,尝试使用更具体的xpath,而不仅仅是td[5]或类似的smthn,例如上面的示例可能如下:
//div[@class='dt\u padded\u medium\u div']/a[@href]
-意思是,我们需要找到具有特定父元素的
元素,并且在该元素中
元素具有
href
属性您确定您的xPath是正确的吗

在Firefox上安装FirePath,并在下面的屏幕截图上试验xPath

而且,这看起来确实像一个漫长而脆弱的xPath。查看以了解如何使xPath更加健壮

在你的情况下,我会想象:

 //*[@id='block-2']/descendant::a[text()='Last Month']

(获取具有特定ID的元素,然后搜索

在您的问题中,您使用了代码中未显示的“ID”,其中是@ID='block-2'。您可能使用了错误的ID。为了帮助您,您能否提供完整的HTML代码

根据您提供的信息,我可以提出以下建议:

确保您提供的Xpath是唯一的,您可以使用Firefox的附加组件Firebug。它将帮助您快速、轻松地找到所需的Xpath。您需要做的基本上是:

  • 下载并安装firefox
  • 下载firebug插件并安装到firefox
  • 您会注意到工具栏右上角有一个小bug符号,请启用它
  • 你会看到一个控制台弹出,点击inspection按钮并点击你想要检查的任何web元素,其html代码将在控制台中突出显示
  • 右键单击突出显示的代码并选择将其xpath复制到剪贴板。这样,您将永远不会弄错xpath
下面是一个快速教程:

P.P.S.有一种以上的方法来定位WebLIST,请考虑使用以下选项:

  • Css选择器
  • 类名
  • 身份证
这个链接将引导你找到一个很棒的骗子。


希望有帮助。

请尝试使用下面的代码单击上个月的文本

driver.findElement(By.xpath(".//a[text()='Last Month']")).click();

希望这有帮助。

您可以使用下面的代码单击包含文本“Last Month”的链接

driver.findElement(By.linkText("Last Month")).click();

有一些可能性。 如果你的网站没有被重写太多,你可以通过

driver.findElement(By.linkName("Last Month");
如果您的站点被多次这样的重写,您也可以选择任何属性,只需在调用时将“href”和“#”或“onclick”和“setLastMonth()”作为参数

static WebElement getLink(String Attribute, String Value /*String ItemText*/){
List<WebElement> Elements = driver.findElements(By.tagName("a"));
    for(int Counter = 0, Counter < Elements.size(); Counter++){
        if(Elements.get(Counter).getAttribute(Attribute).contains(Value) /* && Elements.get(Counter).getText().equals(ItemText){
            return Elements.get(Counter);
        }
    }
return null;
}
静态WebElement getLink(字符串属性,字符串值/*String ItemText*/){
列表元素=driver.findElements(按.tagName(“a”));
对于(int Counter=0,Counter

如果你删除评论,它将完全确定并检查属性和显示的文本。

谢谢你提供的信息,我已经成功了,但真的很感谢你的帮助!!谢谢Stritter,我使用了这种方法,而且成功了,非常感谢!非常感谢你提供的信息,一定会将其用于将来的参考!非常感谢PreciatedHanks Karthik,使用了你的代码,它工作正常。非常感谢!嘿,Karthik,有可能再问你一个关于selenium webdriver命令的问题吗?是的,请..你可以问。但是根据规则,你需要为这个问题启动新的线程,我猜..)你问过这个问题吗?如果是的话,你能给我链接吗?是的,给你,请分享你的解决方案;)奇怪的是,我一直在运行firebug,甚至在文本中使用特定的xpath,它仍然不起作用。所以我使用了“driver.findElement(By.xpath(“.//a[text()='Last Month']”)