【问题标题】:Which locator I can use to click the span element through selenium我可以使用哪个定位器通过 selenium 单击 span 元素
【发布时间】:2019-02-13 18:35:25
【问题描述】:

我可以在下面使用哪个定位器?

<span class="tile-name ng-binding">Payment Partner</span>

来自 OP 的 cmets 的更新:

代码试用 1:

driver.findElementByXPath("//span[text()='Payment Partner']").click(); 

错误:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible;

代码试用 2:

driver.findElementByCssSelector("Payment Partner").click(); 

错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"Payment Partner"}

代码试用 3:

WebDriverWait wait = new WebDriverWait(driver, 20);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath ("//span[text()='Payment Partner']')")));

【问题讨论】:

  • 分享您的 XPath 和 CSS 选择器
  • 以文本格式或您尝试过的url和代码分享html
  • @Andersson 请检查我的新更新
  • @sers 请检查我的新更新
  • @Catia 你也应该分享你的代码尝试

标签: java selenium xpath css-selectors webdriver


【解决方案1】:

根据您共享的 HTML 来查找 &lt;span&gt; 标记,因为该元素是 Angular 元素,您必须为 WebDriverWait 诱导 元素可点击,您可以使用以下任一解决方案:

  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='tile-name ng-binding' and contains(.,'Payment Partner')]"))).click();
    
  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.tile-name.ng-binding"))).click();
    

更新

根据错误更新调用click(),您可以使用以下解决方案:

  • xpath:

    WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='tile-name ng-binding' and contains(.,'Payment Partner')]")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", myElement); 
    

您可以在Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:找到详细讨论

【讨论】:

  • 感谢您的建议,但上述方法均无效,请参阅错误:线程“main”中的异常 org.openqa.selenium.WebDriverException:未知错误:元素 ... 在点 (152, 202) 不可点击。其他元素会收到点击:...(会话信息:chrome=68.0.3440.106)
  • @Catia 查看我的答案更新并让我知道状态。
  • 它适用于更新后的解决方案.... 干得好,非常感谢,在这个问题上卡了好几天。
【解决方案2】:

以下是可用于上述 HTML 行的变化:

  1. Xpath://span[text()='Payment Partner']

  2. Xpath : //span[@class='tile-name ng-binding']

  3. Xpath://span[contains(@class,'tile-name')]

  4. Xpath : //span[contains(@class,'ng-binding')]

  5. Xpath://span[contains(@class,'tile-name') and text()='Payment Partner']

  6. CssPath : span[class='tile-name ng-binding']

如果有父元素,我可以添加更多。

希望这有帮助。

【讨论】:

  • 您能再分享一些 HTML 代码吗?还有你的硒代码?它将帮助我们发现错误。
  • 请查看我之前分享的cmets。
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 2017-11-23
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多