【问题标题】:Selenium: Click on tag by title using XPathSelenium:使用 XPath 按标题单击标签
【发布时间】:2020-01-21 07:54:31
【问题描述】:

在 Xpath 上需要一些帮助以获取以下代码:

<span class-"metadata-row float-left" style="width: 1.9vw;"> &absp; </span>
<input placeholder="New Course Name" id="newCourseName" type="text” class="metadata-name metadata-name-edit font-12" autofocus>
<span class="fa fa-check metadata-action-icon" title="Save" onclick="addCourse(this)" style="display: block;"> ... </span>

我想点击代码中作为标题提到的“保存”,但在 ui 中它显示为图标。

【问题讨论】:

  • 将代码添加为文本而不是图像链接。
  • 是的,谢谢。我会好好照顾的

标签: java selenium selenium-webdriver xpath


【解决方案1】:

title属性为Save的元素上click()需要使用elementToBeClickable(),可以使用以下Locator Strategies之一:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.fa.fa-check.metadata-action-icon[title='Save']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='fa fa-check metadata-action-icon' and @title='Save']"))).click();
    

【讨论】:

  • “谢谢”,我尝试了第一个,但在线程“main”org.openqa.selenium.TimeoutException 中出现异常异常:预期条件失败:等待元素可点击:By.cssSelector: span.fa.fa-check.metadata-ok.metadata-action-icon[title='Save'](尝试 20 秒,间隔 500 毫秒)
  • @virsoftech 似乎您已将 html 图像替换为基于文本的 HTML,并且 DOM 已更改。我已经相应地更新了答案。
  • 先生,我得到:线程“主”org.openqa.selenium.TimeoutException 中的异常:预期条件失败:等待元素可点击:By.xpath://span[@class='在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java: 95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at test.Utils.saveIcon(Utils.java:51) at test.AdminHomePage.questionWindow(AdminHomePage.java:144) at test.MyClass.main(MyClass.java:59)
【解决方案2】:

试试 javascript 执行器

((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    相关资源
    最近更新 更多