Selenium C# 选择 div 内的元素

huangapple go评论55阅读模式
英文:

Selenium C# select an element inside div

问题

以下是代码部分的翻译,不包括代码本身:

  1. WebElement element = (WebElement)driver.FindElement(By.XPath(...)); - 通过XPath查找元素。

  2. Helper.JavaScriptClick(_driver, element); - 使用JavaScript执行点击操作。

  3. WebElement elementValue = (WebElement)driver.FindElement(By.XPath(...)); - 通过XPath查找另一个元素。

  4. Helper.JavaScriptClick(_driver, elementValue); - 使用JavaScript执行点击操作。

英文:

Hello all I have a select where it was binded as follows in UI

<div class="choices form-group form-choices" data-type="select-one" dir="ltr" tabindex="-1" role="listbox" aria-haspopup="true" aria-expanded="false">
 <div class="form-control ui fluid selection dropdown" tabindex="0">
  <select lang="en" class="form-control choices__input" type="text" name="data[serviceAmpsDelivered]" ref="selectContainer" dir="ltr" hidden="" tabindex="-1" data-choice="active">
   <option value="6"><span>60</span></option>
  </select>
  <div class="choices__list choices__list--single">
   <div class="choices__item choices__item--selectable" data-item="" data-id="38" data-value="6" data-custom-properties="null" aria-selected="true" data-deletable="">
    <span>60</span>
    <button type="button" class="choices__button" aria-label="Remove item: '6'" data-button="">Remove item</button>
   </div>
  </div>
 </div>
 <div class="choices__list choices__list--dropdown" aria-expanded="false">
  <div class="choices__list" role="listbox">
   <div id="choices--data-oa-item-choice-1" class="choices__item choices__item--choice choices__item--selectable is-highlighted" role="option" data-choice="" data-id="1" data-value="1" data-select-text="" data-choice-selectable="" aria-selected="true">
    <span>50</span>
   </div>
   <div id="choices--data-oa-item-choice-2" class="choices__item choices__item--choice choices__item--selectable " role="option" data-choice="" data-id="2" data-value="6" data-select-text="" data-choice-selectable="">
    <span>60</span>
   </div>
   <div id="choices--data-oa-item-choice-3" class="choices__item choices__item--choice choices__item--selectable " role="option" data-choice="" data-id="3" data-value="2" data-select-text="" data-choice-selectable="">
    <span>100</span>
   </div>
   <div id="choices--data-oa-item-choice-4" class="choices__item choices__item--choice choices__item--selectable " role="option" data-choice="" data-id="4" data-value="5" data-select-text="" data-choice-selectable="">
    <span>150</span>
   </div>
   <div id="choices--data-oa-item-choice-5" class="choices__item choices__item--choice choices__item--selectable " role="option" data-choice="" data-id="5" data-value="3" data-select-text="" data-choice-selectable="">
    <span>200</span>
   </div>
   <div id="choices--data-oa-item-choice-6" class="choices__item choices__item--choice choices__item--selectable " role="option" data-choice="" data-id="6" data-value="4" data-select-text="" data-choice-selectable="">
    <span>400</span>
   </div>
  </div>
 </div>

I am able to select the div and the dropdown gets opened but I am unable to select the item

WebElement element= (WebElement)driver.FindElement(By.XPath("/html/body/div[1]/div/div[1]/app/div/div[2]/div[2]/div/div[2]/app/div/div/div/div[2]/div[2]/div[2]/app/div/div/div/forms-app/div/div/div/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]"));
Helper.JavaScriptClick(_driver, element);

WebElement elementValue= (WebElement)driver.FindElement(By.XPath("/html/body/div[1]/div/div[1]/app/div/div[2]/div[2]/div/div[2]/app/div/div/div/div[2]/div[2]/div[2]/app/div/div/div/app/div/div/div/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/div/div[4]"));
        Helper.JavaScriptClick(_driver, elementValue);

答案1

得分: 0

如果您想从下拉列表中选择项目,可以检查如果使用父标签作为div,然后以下是子标签,ID对于每个元素是否是唯一的。

项目1的`xpath`。

    //div[@id='choices--data-oa-item-choice-1']/span

项目2的`xpath`。

    //div[@id='choices--data-oa-item-choice-2']/span
等等。


----------

如果您想选择特定项,可以将值参数化。假设您想从下拉列表中选择`150`。

    var itemval="150";
    WebElement element=(WebElement)driver.FindElement(By.XPath($"//div[starts-with(@id,'choices--data-oa-item-choice-')]/span[text()='{itemval}']"));
    Helper.JavaScriptClick(_driver, element);
英文:

If you would like to select item from dropdown list, You
can check the ID is unique for each elements if you use the parent tag as div and then following child.

xpath for item 1.

//div[@id='choices--data-oa-item-choice-1']/span

xpath for item 2.

//div[@id='choices--data-oa-item-choice-2']/span

and so on.


If you want to select a specific one you can parameterize the value.
Lets say you want to select 150 from dropdown list.

var itemval="150";
WebElement element= (WebElement)driver.FindElement(By.XPath($"//div[starts-with(@id,'choices--data-oa-item-choice-')]/span[text()='{itemval}']"));
Helper.JavaScriptClick(_driver, element);

答案2

得分: 0

OK,经过一些尝试和错误,我找到了解决方案。

WebElement ddlControl = (WebElement)_driver.FindElement(By.XPath("//select[contains(@name,'controlName')));
Helper.JavaScriptClick(_driver, ddlControl);
TimeSpan timeToWait = new TimeSpan(0, 0, 10);
WebDriverWait waitForElemet = new WebDriverWait(_driver, timeToWait);
waitForElemet.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(@id,'ControlId')]//following::span[text()='50']")).Click();
英文:

OK I got the solution after some trail and errors

WebElement ddlControl= (WebElement)_driver.FindElement(By.XPath("//select[contains(@name,'controlName"));
Helper.JavaScriptClick(_driver, ddlControl);
TimeSpan timeToWait = new TimeSpan(0, 0, 10);
WebDriverWait waitForElemet = new WebDriverWait(_driver, timeToWait);
waitForElemet.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(@id,'ControlId')]//following::span[text()='50']"))).Click();

huangapple
  • 本文由 发表于 2023年3月3日 21:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定