【问题标题】:Selenium click the span below the element I can findSelenium 单击我可以找到的元素下方的跨度
【发布时间】:2018-06-01 20:39:23
【问题描述】:

我目前正在尝试为我们在工作中创建的产品创建一个 Selenium 框架。我正在努力在下面找到一个单选按钮:

<input type="button" id="f_pAAF60F411E5E4609865C3F6B0923977D45_3_1_0-inputEl" name="boolean_45" data-ref="inputEl" fieldref="PolicyInput.GoingOnCruise" objectref="pAAF60F411E5E4609865C3F6B0923977D" tabindex="0" class="x-form-cb-input x-hidden-clip" autocomplete="off" hidefocus="true" role="radio" aria-hidden="false" aria-disabled="false" aria-readonly="false" aria-invalid="false" aria-checked="false" aria-labelledby="f_pAAF60F411E5E4609865C3F6B0923977D45_3_1_0-boxLabelEl" data-componentid="f_pAAF60F411E5E4609865C3F6B0923977D45_3_1_0">
<span id="f_pAAF60F411E5E4609865C3F6B0923977D45_3_1_0-displayEl" data-ref="displayEl" class="x-form-field x-form-radio x-form-radio-default x-form-cb x-form-cb-default "></span>

使用产品让我能够单击收音机,但我需要能够单击 span 元素。但是,我面临的问题是在元素上; id 是每个会话随机生成的,data-refclass 与其他随机按钮通用。

下面好像没有找到span元素。

[FindsBy(How = How.XPath, Using = "//input[ends-with(@id,'0-inputEl')][@fieldref='PolicyInput.GoingOnCruise']/span")]
private IWebElement CruiseYes;

有没有办法选择跨度元素。例如,使用输入元素中的fieldref,然后使用span 元素中的0-displayEl,然后单击span 元素。

【问题讨论】:

    标签: c# html selenium


    【解决方案1】:

    试试下面的代码块:

    [FindsBy(How = How.XPath, Using = "//input[ends-with(@id,'inputEl') and starts-with(@name,'boolean')]/span[@class='x-form-field x-form-radio x-form-radio-default x-form-cb x-form-cb-default']")]
    private IWebElement CruiseYes;
    

    【讨论】:

      【解决方案2】:

      使用以下逻辑:

      1. 创建具有data-refclass 属性的所有Web 元素(跨度)的列表。

      2. 从列表中按索引点击需要的。

      【讨论】:

      • 我有一个元素列表List&lt;IWebElement&gt; elements = ObjectRepository.Driver.FindElements(By.XPath("//span[@data-ref='displayEl']")).ToList();。我如何点击第一个,第二个等?
      • elements[index].click(),其中index 是列表中元素的索引(列表中的索引从 0 开始)。我已经更新了我的答案。
      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 2022-12-29
      • 2021-12-26
      • 2019-10-28
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多