Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Java 如何通过xpath获取标题?_Java_Xpath_Selenium - Fatal编程技术网

Java 如何通过xpath获取标题?

Java 如何通过xpath获取标题?,java,xpath,selenium,Java,Xpath,Selenium,有没有办法用xpath代替driver.getTitle() 我尝试了以下方法: By.xpath(“//title”) 但它没有起作用。例如StackOverflow public class TestStack { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "chromedriver\\chromedriver.exe");

有没有办法用xpath代替
driver.getTitle()

我尝试了以下方法:
By.xpath(“//title”)

但它没有起作用。例如StackOverflow

public class TestStack {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "chromedriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://stackoverflow.com/");
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//title")));

        System.out.println(driver.findElement(By.xpath("//title")).getText());
    }

}

未找到xpath中的元素

我看不出您不想使用
driver.getTitle()的原因

但如果必须使用xpath,则可以执行以下操作:

driver.findElement(By.xpath("//title")).getText();
更新

根据最新的问题看来,失败就在眼前

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//title")));
要修复它,我建议等待的时间超过
0
秒,并进行更改
已定位元素的可视性
已定位元素的存在性

理想情况下,您应该使用
ExpectedConditions.titleIs()

可以找到有关可用选项的更多信息。

我看不出您不想使用
driver.getTitle()的原因。

但如果必须使用xpath,则可以执行以下操作:

driver.findElement(By.xpath("//title")).getText();
更新

根据最新的问题看来,失败就在眼前

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//title")));
要修复它,我建议等待的时间超过
0
秒,并进行更改
已定位元素的可视性
已定位元素的存在性

理想情况下,您应该使用
ExpectedConditions.titleIs()

有关可用选项的更多信息,请参见。

问题似乎与浏览器有关。下面的代码在FF 3.6上生成了正确的输出,但在IE 9和Chrome 31中都没有这样做(空字符串)

WebElement title = driver.findElement(By.xpath("//title"));         
System.out.println(title.getText());
如果您真的不想使用
getTitle()
这应该适用于任何浏览器:

title.getAttribute("innerHTML");

问题似乎与浏览器有关。下面的代码在FF 3.6上生成了正确的输出,但在IE 9和Chrome 31中都没有这样做(空字符串)

WebElement title = driver.findElement(By.xpath("//title"));         
System.out.println(title.getText());
如果您真的不想使用
getTitle()
这应该适用于任何浏览器:

title.getAttribute("innerHTML");

请给出html。是否有理由不想使用
driver.getTitle()
?我对@Amey question的答案感兴趣请给出html。是否有理由不想使用
driver.getTitle()
?我对@Amey questionOP的答案感兴趣我想有一些隐藏的事情,因为他没有使用直接方法:)另一种方法是
driver.findElement(By.xpath(“//title/text()”)
。。。我知道ruby在哪里可以像
driver.find\u元素(“//title/text()”)一样运行。我不知道java的等价物。不幸的是,这个解决方案没有做到这一点。我已经试过了,请看一下我上面更新的问题,我在哪里尝试从StackOverflow获取标题?你有什么错误?我会在
WebDriverWait wait=newwebdriverwait(driver,0)中等待比
0
更多的时间这是一个打字错误,我已经改正了。我正在等待60秒:WebDriverWait wait=newwebdriverwait(driver,60);获取TimeoutExceptionOP我认为有一些隐藏的问题,因为他没有使用直接方法:)另一种方法是
driver.findelelement(By.xpath(“//title/text()”)
。。。我知道ruby在哪里可以像
driver.find\u元素(“//title/text()”)一样运行。我不知道java的等价物。不幸的是,这个解决方案没有做到这一点。我已经试过了,请看一下我上面更新的问题,我在哪里尝试从StackOverflow获取标题?你有什么错误?我会在
WebDriverWait wait=newwebdriverwait(driver,0)中等待比
0
更多的时间这是一个打字错误,我已经改正了。我正在等待60秒:WebDriverWait wait=newwebdriverwait(driver,60);并获取TimeoutException