【问题标题】:How to click on element which is cloned, in Selenium Webdriver?如何在 Selenium Webdriver 中单击克隆的元素?
【发布时间】:2016-10-03 02:37:30
【问题描述】:

请检查我需要单击开始试用按钮的图像,该按钮处于循环模式,通过单击右箭头或左箭头,我将多次使用其他选项。

 form method="post" name="" action="http://dev-imaginovation.net/100s-happiness/www-100s-happiness/virtualclasspay">
<input id="pkg_id" type="hidden" value="3" name="pkg_id"/>
<input id="pkg_price" type="hidden" value="0" name="pkg_price"/>
<div class="virtualplan_box">
<div class="virtualplanHeader">Free Package</div>
<div class="virtualplanContent">
<h3>Free Subscription</h3>
<p>Check one of our On-the-Go Virtual Classes free of charge!</p>
<div class="virtualplanprice">
<div id="virtual_description" class="learn-more getstart-btn wow zoomIn clearfix animated" style="visibility: visible; animation-name: zoomIn;">
<a class="fr-btn orange-color-bg white-color purchase-btn" name="submit_plan" style="color: rgb(255, 255, 255); background-color: rgb(247, 144, 73);" href="http://dev-imaginovation.net/100s-happiness/www-100s-happiness/freevirtualclass">Start Trial</a>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="owl-item cloned" style="width: 670px; margin-right: 0px;">
<div class="owl-item active" style="width: 670px; margin-right: 0px;">
<div class="owl-item" style="width: 670px; margin-right: 0px;">
<div class="owl-item cloned" style="width: 670px; margin-right: 0px;">
<div class="owl-item cloned" style="width: 670px; margin-right: 0px;">

【问题讨论】:

  • 请同时发布源代码
  • 你有没有在你克隆后把你的点击事件绑定到按钮上?
  • 你可以在底部看到3个匹配的节点,在附图中重复出现
  • 嘿,您确定的定位器存在问题,它提供了 3 个匹配节点。他们三个都有相同的xpath。您能告诉我们您具体要寻找哪一个(1、2 或 3)。如果可能,请分享相关的 HTML。
  • 嗨,我正在寻找第一个。让我检查并发送代码

标签: selenium-webdriver


【解决方案1】:

尝试下面的 xpath 索引:-

// for first element
(//div[@id = 'virtual_description']/a)[1]

// for second element
(//div[@id = 'virtual_description']/a)[2]

// for third element
(//div[@id = 'virtual_description']/a)[3]

希望对你有帮助..:)

【讨论】:

  • 感谢 Omi 的回复,但它不起作用 - 即使我能够查看屏幕上的按钮,显示元素也不可见
  • @nic.jess 你试过这个xpath来定位firepath上的元素吗???..首先确保它..如果你找到你想要的元素然后尝试使用WebDriverWait查找元素..
  • 是的,但是当我将你给的 xpath 保存在 Firepath 中时,该按钮没有突出显示
  • 删除引号...仅粘贴 (//div[@id = 'virtual_description']/a)[1]
  • 当然奥米,我也做了同样的事情,但按钮仍然没有突出显示
【解决方案2】:

试试下面的:

List<WebElement> elem = driver.findElements(By.cssSelector("div#virtual_description[style*='visibility: visible']>a.fr-btn"));

    for(int i=0;i<elem.size();i++){
        try {
            if(elem.get(i).isDisplayed()){//u can try isEnabled() here
                elem.get(i).click();
                break;

            }
        } catch (Exception e) {             
            continue;
        }

    }

希望这行得通。

【讨论】:

  • 您好,谢谢您的回复,但是您提供的代码也不起作用检查下面的回复:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法":"css 选择器","selector":"div#virtual_description[style='visibility: visible']>a.fr-btn"}
  • 抱歉,新的也不能用。你能检查一下代码吗?你认为我们需要提供:driver.findElement(By.tagName("form"));在为开始试用按钮编写代码之前
  • 你能帮我做吗....用chrome打开它...打开开始试用框后,用右键检查它,而不是按cntrl+f,那里写div# virtual_description[style*='visibility: visible']>a.fr-btn 请告诉我它给出了多少建议
猜你喜欢
  • 2020-12-22
  • 2020-07-27
  • 2017-12-02
  • 2014-04-02
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多