【问题标题】:How to locate the object in selenium如何在硒中定位对象
【发布时间】:2014-09-24 14:01:02
【问题描述】:

我无法单击 2 个图标之间的图标,因为它们具有相同的类名,尽管它有两个不同的 xpath,但 xpath 没有内容处理程序,因此 findElement(By.xpath()) 注释不起作用。下面是 HTML 的区别

class="doughnut-clicker-circle" questiontype="hypothetical" 

class="doughnut-clicker-circle" questiontype="multichoice"

唯一的区别是问题类型。谁能告诉我如何在 selenium + Java 中找到元素。

<svg>
<circle class="doughnut-clicker-circle" questiontype="multichoice" learningobjid="dfe00abe-cef8-11e3-8be9-67ee60a9f4aa" learningobjname="Determine whether trespassers have a right to capture property." data-action="/assessmentRetrievalService/getQuestionForStudyCenterLearn" data-handler="loadStudyCenter" fill="transparent" r="48" cy="53" cx="53">
Sorry, your browser does not support inline SVG.
</svg>

<svg>
<circle class="doughnut-clicker-circle" questiontype="hypothetical" learningobjid="dfe00abe-cef8-11e3-8be9-67ee60a9f4aa" learningobjname="Determine whether trespassers have a right to capture property." data-action="/assessmentRetrievalService/getQuestionForStudyCenterLearn" data-handler="loadStudyCenter" fill="transparent" r="48" cy="53" cx="53">
Sorry, your browser does not support inline SVG.
</svg>

【问题讨论】:

    标签: java selenium xpath selenium-webdriver


    【解决方案1】:

    您可以使用标识符的组合找到这些元素。假设按类和属性值,例如//*[@class='doughnut-clicker-circle' and @questiontype='hypothetical']//*[@class='doughnut-clicker-circle' and @questiontype='multichoice']

    【讨论】:

    • 能够启动Hypo问题但不能使用相同的代码进行午餐多项选择。 driver.findElement(By.xpath(".//*[@class='doughnut-clicker-circle' and @questiontype='multichoice']")).click();
    • 您确定单击“Hypo”元素后元素“Multiple”没有改变吗?
    • 我正在运行两个不同的测试用例来进行多项选择和hypo。所以当我输入 driver.findElement(By.xpath(".//*[@class='doughnut-clicker-circle' and @questiontype='multichoice']")).click();它找不到元素。
    • 如果您将与“hypo”相关的测试作为第一个测试呢?该元素是否位于?
    【解决方案2】:

    将发布 Java 代码,因为我在 Java 下使用 Selenium(希望这对您的情况有所帮助):

    @FindBy(how = How.CSS, using = "circle[questiontype*='multichoice']")
    private WebElement circle;
    

    基本上,你需要在下面的CSS之后找到一个元素:

    "circle[questiontype*='multichoice']"

    意思是circle标签具有questiontype属性,值为multichoice(该值必须包含字符串multichoice不等于它)。

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2017-01-11
      • 2020-08-26
      • 2011-01-27
      • 1970-01-01
      相关资源
      最近更新 更多