使用Selenium(Java)与React下拉列表进行交互:“其他元素将获得点击”

时间:2019-10-01 09:29:54

标签: java reactjs selenium

我在与React应用程序下拉框进行交互时遇到一些困难。

我只需要按定位器选择一个下拉列表,然后选中和取消选中各个复选框即可。

以下代码段是其中5个此类框中的1个示例:

<div class="MuiFormControl-root jss55" xpath="1">
<label class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined" data-shrink="false" for="sectorDescription">Sector</label>
<div class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-formControl">
    <div class="MuiSelect-root MuiSelect-select MuiSelect-selectMenu MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputSelect MuiOutlinedInput-inputSelect MuiSelect-outlined" tabindex="0" role="button" aria-haspopup="listbox"><span>&#8203;</span></div>
    <input type="hidden" id="sectorDescription" value="">
    <svg class="MuiSvgIcon-root MuiSelect-icon" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
        <path d="M7 10l5 5 5-5z"></path>
    </svg>
    <fieldset aria-hidden="true" class="jss131 MuiOutlinedInput-notchedOutline" style="padding-left: 8px;">
        <legend class="jss132" style="width: 0.01px;"><span>&#8203;</span></legend>
    </fieldset>
</div>

因此,要选择此第一个复选框,我首先需要单击。我试过选择器: CSS:

label[for='sectorDescription']

xpath:

//label[contains(text(),'Sector')]    

我的代码:

//Test script    
pricesPage.selectSectorDropDown();

//Page Object
public void selectSectorDropDown(){
waitForIsClickable(sectorDropDown, 20);
click(sectorDropDown);
}

//BasePage
protected Boolean waitForIsClickable(By locator, Integer... timeout) {
    try {
        waitFor(ExpectedConditions.elementToBeClickable(locator),
                (timeout.length > 0 ? timeout[0] : null));
    } catch (org.openqa.selenium.TimeoutException exception) {
        return false;
    }
    return true;
}

两者都产生:

element click intercepted: Element <label class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined" data-shrink="false" for="sectorDescription">...</label> is not clickable at point (157, 273). Other element would receive the click:

我正在使用自定义的书面Wait方法来确保该元素是可单击的。我需要其他策略,但不确定该怎么做,因为我不确定为什么该元素不可点击?

1 个答案:

答案 0 :(得分:0)

看起来您要单击的元素部分隐藏在其他元素的后面。硒在元素的某些坐标处单击。您应该分析哪个元素与您的目标重叠以及哪个区域重叠。然后:

  1. 由于重叠的元素可能是由较小的视口引起的,因此任何一个都尽可能尝试增加窗口大小(例如,最大化)
  2. 或检查目标元素中是否存在未被其他元素覆盖的部分,将光标移至该部分(您必须评估所需的偏移量),然后单击Actions链。