gpt4 book ai didi

reactjs - 使用Selenium从输入组合框中选择一个值,并使用xpath进行reachjs控件

转载 作者:行者123 更新时间:2023-12-03 16:15:19 26 4
gpt4 key购买 nike

您好需要使用By.xpath()选择“新南威尔士州”吗?

这是一个reactjs页面jedwatson.github.io/react-select

<div class="Select-control">
<span class="Select-multi-value-wrapper" id="react-select-2--value">
<div class="Select-value State-NSW">
<span class="Select-value-label" role="option" aria-selected="true" id="react-select-2--value-item">New South Wales</span>
</div>
<div class="Select-input" style="display: inline-block;">
<input role="combobox" aria-expanded="false" aria-owns="" aria-haspopup="false" aria-activedescendant="react-select-2--value" value="" style="width: 5px; box-sizing: content-box;">
<div style="position: absolute; top: 0px; left: 0px; visibility: hidden; height: 0px; overflow: scroll; white-space: pre; font-size: 14px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: normal; font-style: normal; letter-spacing: normal;"></div>
</div>
</span>
<span class="Select-clear-zone" title="Clear value" aria-label="Clear value">
<span class="Select-clear">×</span>
</span>
<span class="Select-arrow-zone">
<span class="Select-arrow"></span>
</span>
</div>


尝试使用以下代码未成功

driver.findElement(By.xpath("//input[@role='combobox'][@value='New York"']")).click();;


观察到以下异常

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@role='combobox'][@value='New York']"}
(Session info: chrome=59.0.3071.86)

最佳答案

请按照以下步骤选择所需的元素


单击下拉菜单的向下箭头
选择所需的选项


这是相同的代码:

driver.findElement(By.xpath("//h3[contains(text(),'States')]/..//span[@class='Select-arrow']")).click();
driver.findElement(By.id("react-select-2--option-1")).click();


更新

使用以下方法根据文本动态选择值:

public void selectCombo(String valueText)
{
driver.findElement(By.xpath("//h3[contains(text(),'States')]/..//span[@class='Select-arrow']")).click();
WebElement dropdownValue = driver.findElement(By.xpath("//div[contains(text(),'"+valueText+"')]"));
dropdownValue.click();
}


从您的代码中调用此方法并传递您要选择的值

例如

new TestClass().selectCombo("Tasmania");

关于reactjs - 使用Selenium从输入组合框中选择一个值,并使用xpath进行reachjs控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45668983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com