【问题标题】:How to differentiate 2 html classes with same name when testing them with Selenium使用 Selenium 测试它们时如何区分 2 个具有相同名称的 html 类
【发布时间】:2019-09-30 03:59:07
【问题描述】:

我正在黑盒测试一个网页作为我的学校项目。我正在浏览网站,检查功能,并想在工作部分选择一份工作,并通过填写表格申请该工作。 现在,目前有两个职位,它们属于同名类别。我可以打开两个工作中的第一个,但不知道如何导航到第二个,因为它们的名称相同,所以我的 Selenium Web Driver 测试总是打开第一个,我想要第二个。 我在 Selenium Web Driver, c# 中工作。

<h4 class="job-section-title">Development / Belgrade</h4>
<div class="job-section">

<div class="job">
<h3>Software engineer</h3>
<div class="job-description">


<div class="job">
<h3 class="">Test engineer</h3>
<div class="job-description">

我尝试使用此功能获得第二份工作: //var selectElement = new SelectElement(driver.FindElement(By.ClassName("Job")));

//selectElement.SelectByText("Test engineer");

没用。 我希望提供所有必要的信息。 提前谢谢你。

【问题讨论】:

  • 用 HTML 更新问题,用于 两个职位,包括父节点。

标签: html selenium automated-tests


【解决方案1】:

尝试使用以下 xpath 来单击元素。

driver.FindElement(By.XPath("//div[@class='Job']/h3[contains(.,'Test engineer')]")).Click();

【讨论】:

    【解决方案2】:

    最后我发现没有必要以我想要的方式区分 html 类。 Selenium 也使用 css 和 xpath 来完成这项工作。 所以我简单地复制了 xpath(或 css 选择器)并将其添加到我的代码中,如下所示: driver.FindElement(By.Xpath(xpath 链接)) driver.FindElement(By.CssSelector(选择器链接))

    谢谢大家的帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多