【问题标题】:selenium: how to click a page linkselenium:如何单击页面链接
【发布时间】:2017-12-15 14:48:45
【问题描述】:

我正在使用带有 selenium-webdriver 的黄瓜,但是单击页面上的链接不起作用。

HTML 代码:

  <li id="menu-item-325" class="menu-item menu-item-type-post_type menu-item-object-page  menu-item-325">
  <a href="mysite/contact/" class="nav-top-link">Contact</a>
 </li>

测试代码:

const { defineSupportCode } = require('cucumber');
const { Builder, By } = require('selenium-webdriver');

const driver = new Builder()
    .forBrowser('chrome')
    .build();

defineSupportCode(function ({ Given, When, Then }) {
  // Navigation to the url works
  Given('I am on the site homepage', function () {
    return driver.get(url);
  });

  When('When I click Contact', function() {
    return driver.findElement(By.linkText('Contact')).click();
  });
});

我也试过了:

return driver.findElement(By.xpath("//a[@href='mysite/contact/']")).click();

【问题讨论】:

    标签: selenium cucumber


    【解决方案1】:

    有时在点击工作之前提供睡眠。尝试一次等待或 Thread.sleep

    由于正常点击不起作用,即使在睡眠状态下,请尝试

    使用 moveToElement 进行操作,然后单击。

    您也可以在 final 中尝试使用 javascript。

    WebElement element = driver.findElement(By.id("gbqfd"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element);
    

    【讨论】:

      【解决方案2】:

      要点击测试为 Contact 的链接,您可以使用以下代码行:

      driver.findElement(By.xpath("//li[@class='menu-item menu-item-type-post_type menu-item-object-page' and starts-with(@id, 'menu-item-')]/a[@class='nav-top-link']")).click();
      

      我们可以更详细一点:

      driver.findElement(By.xpath("//li[@class='menu-item menu-item-type-post_type menu-item-object-page' and starts-with(@id, 'menu-item-')]/a[@class='nav-top-link' and @href='mysite/contact/']")).click();
      

      注意:click() 方法返回 void

      【讨论】:

      • 它不起作用。如何链接多个类名? "//li[@class='menu-item menu-item-type-post_type 对吗!
      • @user3462064 代码没有错误。您已经变形了实际的 HTML。因此对你不起作用。向我们展示更多outerHTML
      • li 标签中id="menu-item"class="menu-item" 不能相同。
      • 代码没有错误,您一定错过了提供的 HTML 中的某些属性或您需要显示的 outerHTML 中的某些内容。
      【解决方案3】:

      简单使用xpaths

      喜欢这个

      //a[contains(text(),'Contact')]
      

      代码:

      driver.findElement(By.xpath("//a[contains(text(),'Contact')]")).click();
      

      //a[contains(text(),'Contact') and contains(@class,'nav-top-link')]
      

      代码:

      driver.findElement(By.xpath("//a[contains(text(),'Contact') and contains(@class,'nav-top-link')]
      ")).click();
      

      也可以试试xpathjavaScriptExecutor的组合

      WebElement elemenLink = driver.findElement(By.xpath("//a[contains(text(),'Contact') and contains(@class,'nav-top-link')]"));
      JavascriptExecutor executor = (JavascriptExecutor)driver;
      executor.executeScript("arguments[0].click();", elemenLink );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-21
        • 2020-09-21
        • 1970-01-01
        • 2020-02-16
        • 2022-01-22
        • 2015-08-18
        • 2013-04-27
        • 2017-02-02
        相关资源
        最近更新 更多