【问题标题】:Selenium, Java - How to click at an element using text?Selenium,Java - 如何使用文本单击元素?
【发布时间】:2015-03-26 09:00:21
【问题描述】:

我在单击一个元素时遇到问题,我发现它使用的是一个变量文本。这是页面的代码:

<div class="recommendedProfileList fl">
<h3>
<ul class="ctrlResearchProfiles">
<li>
<li>
<li>
<li>
<li>
<li>
<span class="profileBtn ctrlSelectDefProfile ctrlClickSubmit" data-value="143" data-form="formChooseProfile" data-profileid="143">Sales manager</span>
<span class="profileTooltip" style="display: none;">
<span class="arrow"/>
<span class="profileTooltipContent">
</span>

变量的名字是profile。这就是我尝试这样做的方式,但没有奏效:

WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()=' + profile + ']")));

秒:

driver.findElement(By.xpath("//*[text()=' + profile + ']"));

还有:

driver.findElement(By.linkText("" +profile)).click();

你知道如何点击这样的元素吗?

【问题讨论】:

  • 您是否尝试单击类“profileBtn”的 span 元素?
  • 是的,我想咨询销售经理。页面上几乎没有熟悉的元素,每个元素的数据值也不同,但我不知道如何使用那个元素,所以我一直在尝试使用名称
  • 您是否调试过代码以查看对 driver.findElement(...) 的调用是否返回正确的 WebElement 实例对象?这可以帮助您找出问题所在。是点击没有效果还是findElement返回了错误的元素?

标签: java testing selenium click webdriver


【解决方案1】:

你就快到了,哥们……

wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='" + profile + "']")));

秒:

driver.findElement(By.xpath("//*[text()='" + profile + "']"));

你错过的是在 xpath 中插入你的变量值的双引号。

【讨论】:

  • 您的提示有效,已找到该元素。原来,元素是不可见的……
  • 对不起,我的错!它完美无缺!非常感谢:)
猜你喜欢
  • 2020-05-02
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2022-12-04
相关资源
最近更新 更多