【问题标题】:Not Able to click on "Search" button Selenium Web Driver Java无法单击“搜索”按钮 Selenium Web Driver Java
【发布时间】:2019-08-19 18:30:58
【问题描述】:

我正在尝试单击“搜索”按钮,但没有成功。我也尝试搜索 iframe,但没有 iframe。

这是我的“搜索”按钮 html 标签:

<input type="submit" value="search">

这是我用来点击搜索按钮的 xpath:

driver.findElement(By.xpath("//*[@id='mainDiv']/table/tbody/tr/td/div[3]/blockquote/fieldset/input[1]")).click();

我尝试过的以下方法:

new Actions(driver).moveToElement("//*[@id='mainDiv']/table/tbody/tr/td/div[3]/blockquote/fieldset/input[1]").perform();

JavascriptExecutor jse = (JavsscriptExecutor)driver; jse.executeScript("document.getElementById(//*[@id='mainDiv']/table/tbody/tr/td/div[3]/blockquote/fieldset/input[1]").focus().click();");

new WebDriverEait(driver, 10).until(ExpectedConditions.ElementToBeClickable(By.cssSelector(Inpout.submit[value='search']"))).click();

driver.findElement(By.cssSelector(".submit[value='search']")).click();

上面的方法应该可以点击“搜索”按钮。

但是,我遇到了异常:无法使用 cssSelector 或 xpath 或 id 找到元素

【问题讨论】:

    标签: java selenium-webdriver


    【解决方案1】:

    为什么使用绝对 xpath 而不是尝试下面的相对 xpath

    driver.findElement(By.xpath("//*[@type='submit'])).click();
    

    【讨论】:

    • 很高兴它对您有所帮助。
    【解决方案2】:

    根据您的“搜索”按钮的 html 标记,它看起来是网页上存在的表单的提交按钮。您可以像普通按钮一样在 Web 元素上使用 click() 方法,也可以在页面上的任何 Web 元素或提交按钮本身上使用 submit() 方法。

    尝试定位 Search(Submit) 元素

    driver.findElement(By.xpath("//*[@type='submit'])).submit();
    

    或者您可以在该页面上的任何其他 web 元素上调用 submit() 方法。

    driver.findElement(By.xpath("//*[@id='some_id'])).submit();
    

    【讨论】:

    • 我都试过了,但都没有奏效。然后我用 .click() 方法尝试了相对路径,它起作用了。
    猜你喜欢
    • 2019-03-02
    • 1970-01-01
    • 2022-10-03
    • 2015-07-23
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多