Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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
如何使用c#在selenium webdriver ie中实现在网页中查找文本?_C#_Javascript_Selenium - Fatal编程技术网

如何使用c#在selenium webdriver ie中实现在网页中查找文本?

如何使用c#在selenium webdriver ie中实现在网页中查找文本?,c#,javascript,selenium,C#,Javascript,Selenium,我尝试了此代码,但仍有一个错误: IWebDriver driver; driver = new InternetExplorerDriver("C:\\", options); driver.Navigate().GoToUrl("http://www.marathonbet.com/en/"); IJavaScriptExecutor js = driver as IJavaScriptExecutor;

我尝试了此代码,但仍有一个错误:

        IWebDriver driver;
        driver = new InternetExplorerDriver("C:\\", options);

        driver.Navigate().GoToUrl("http://www.marathonbet.com/en/");


        IJavaScriptExecutor js = driver as IJavaScriptExecutor;
        string title = (string)js.ExecuteScript("window.find('Japan',0,0,0,0,0,1)");

所以我想知道这个函数是否有其他选择

如果您只想搜索当前的一些文本,您可以实现如下功能:

boolean isTextPresent(String text){
    return driver.getPageSource().contains(text)
}

为了摆脱另一种方法,请尝试XPath

    String identifierXpath = String.format("//*[contains(text(),'%s')]", searchTerm);

    By identifier = By.xpath(identifierXpath);

    List<WebElement> elementsWithText = driver.findElements(identifier);
String-identifierXpath=String.format(“//*[contains(text(),'%s')]”,searchTerm);
按标识符=按.xpath(identifierXpath);
List elementsWithText=driver.findElements(标识符);
这种方法允许您访问实际包含感兴趣的文本id的元素,当然还可以使用“.size()>0”来确定文本是否确实存在

我可能会在findElements之前将隐式等待时间更改为0秒,然后再重置,但这段代码应该会告诉您我试图理解的想法。显然,如果它是在helper方法中,那么它是有意义的

请注意,我并不是说这比使用“getPageSource()”更好或更有效,它只是解决问题的一种不同方法


一个很大的缺陷是它会区分大小写!但是,您可以将xpath更改为不区分大小写,但这将非常复杂,但可以执行

JavaScript错误(意外的JavaScriptError)这是整个堆栈跟踪后的错误。您也没有使用
驱动程序在任何地方导航。是否有其他选择???我只想实现一个搜索文本功能,发布整个错误。整件事。整个堆栈跟踪。不仅仅是信息。整件事。你想干什么?为什么找不到包含文本Japan的元素?为什么要使用Javascript?你想得到什么确切的信息?您不需要使用Javascript。我正在做的是自动触发浏览器的搜索功能。我如何定位它?就像谷歌浏览器的搜索框?我不明白你的问题。我想实现浏览器搜索框的动作。例如,您将在网页中找到一个文本,然后它将自动向下滚动到文本的位置。我不确定,但是对于元素,您可以使用element.getLocation()然后使用javascript window.scrollTo(x,y)