在 selenium 中单击 select2 下拉列表的问题
Problem with clicking select2 dropdownlist in selenium
我使用 selenium 和 python 编写测试。下拉列表有问题。
我尝试使用直接点击和 javascript executescript 方法,但其中 none 有效。我使用 selenium IDE 来找出 id,css,xpath 等。所以有不止一种选择,我都试过了。
#select_btn = self.driver.find_element_by_css_selector("#select2-2v7k-container")
#select_btn = self.driver.find_element_by_css_selector(".col-md-12")
#select_btn = self.driver.find_element_by_id("select2-2v7k-container")
#select_btn = self.driver.find_element_by_id("select2-t2zd-container")
select_btn = self.driver.find_element_by_xpath("//span[@id='select2-2v7k-container']")
self.driver.execute_script("arguments[0].click();", select_btn)
#select_btn.click()
这里是元素部分(因为是内部产品所以没有public url)
<span _ngcontent-c3="" class="col-md-9 col-sm-8 col-xs-8">
<dashboard-saved-reports-select-component _ngcontent-c3="" _nghost-c5=""><select _ngcontent-c5="" aria-hidden="true" class="col-md-12 col-sm-12 col-xs-12 form-control select2-hidden-accessible" style="width: 100%" tabindex="-1">
<option _ngcontent-c5="" value="-1">Select A saved Report</option>
<optgroup _ngcontent-c5="" label="Reports 1">
<!----><option _ngcontent-c5="" value="447">options1
</option><option _ngcontent-c5="" value="457">options2
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 2">
<!----><option _ngcontent-c5="" value="1020">options1
</option><option _ngcontent-c5="" value="1013">option2
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 3">
</option><option _ngcontent-c5="" value="1035">denemeee
</option><option _ngcontent-c5="" value="1033">deneme-rapor
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 4">
<!----><option _ngcontent-c5="" value="1032">dasd
</option><option _ngcontent-c5="" value="1025">deneeeeee
</option><option _ngcontent-c5="" value="1014">deneme
</option>
</optgroup>
</select><span class="select2 select2-container select2-container--default select2-container--below select2-container--focus" dir="ltr" style="width: 100%;">
<span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1" aria-labelledby="select2-cawo-container">
<span class="select2-selection__rendered" id="select2-cawo-container" title="Select A saved Report">Select A saved Report</span>
<span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span>
<span class="dropdown-wrapper" aria-hidden="true"></span></span>
</dashboard-saved-reports-select-component>
</span>
是否有其他方法或解决方法来检测和打开下拉列表。我找到了一些 javascript 方法来制作可见的下拉菜单,但它没有用。
我检查了 id 部分,发现每次刷新时 id 都在变化。
@id='select2-2v7k-container'.
我发现我可以使用下拉菜单的箭头部分。它只工作一次,但对于我的测试用例来说已经足够了。
#select_btn = self.driver.find_element_by_css_selector(".select2-selection__arrow")
select_btn = self.driver.find_element_by_xpath("//span/span[2]")
我使用 selenium 和 python 编写测试。下拉列表有问题。
我尝试使用直接点击和 javascript executescript 方法,但其中 none 有效。我使用 selenium IDE 来找出 id,css,xpath 等。所以有不止一种选择,我都试过了。
#select_btn = self.driver.find_element_by_css_selector("#select2-2v7k-container")
#select_btn = self.driver.find_element_by_css_selector(".col-md-12")
#select_btn = self.driver.find_element_by_id("select2-2v7k-container")
#select_btn = self.driver.find_element_by_id("select2-t2zd-container")
select_btn = self.driver.find_element_by_xpath("//span[@id='select2-2v7k-container']")
self.driver.execute_script("arguments[0].click();", select_btn)
#select_btn.click()
这里是元素部分(因为是内部产品所以没有public url)
<span _ngcontent-c3="" class="col-md-9 col-sm-8 col-xs-8">
<dashboard-saved-reports-select-component _ngcontent-c3="" _nghost-c5=""><select _ngcontent-c5="" aria-hidden="true" class="col-md-12 col-sm-12 col-xs-12 form-control select2-hidden-accessible" style="width: 100%" tabindex="-1">
<option _ngcontent-c5="" value="-1">Select A saved Report</option>
<optgroup _ngcontent-c5="" label="Reports 1">
<!----><option _ngcontent-c5="" value="447">options1
</option><option _ngcontent-c5="" value="457">options2
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 2">
<!----><option _ngcontent-c5="" value="1020">options1
</option><option _ngcontent-c5="" value="1013">option2
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 3">
</option><option _ngcontent-c5="" value="1035">denemeee
</option><option _ngcontent-c5="" value="1033">deneme-rapor
</option>
</optgroup>
<optgroup _ngcontent-c5="" label="Reports 4">
<!----><option _ngcontent-c5="" value="1032">dasd
</option><option _ngcontent-c5="" value="1025">deneeeeee
</option><option _ngcontent-c5="" value="1014">deneme
</option>
</optgroup>
</select><span class="select2 select2-container select2-container--default select2-container--below select2-container--focus" dir="ltr" style="width: 100%;">
<span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1" aria-labelledby="select2-cawo-container">
<span class="select2-selection__rendered" id="select2-cawo-container" title="Select A saved Report">Select A saved Report</span>
<span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span>
<span class="dropdown-wrapper" aria-hidden="true"></span></span>
</dashboard-saved-reports-select-component>
</span>
是否有其他方法或解决方法来检测和打开下拉列表。我找到了一些 javascript 方法来制作可见的下拉菜单,但它没有用。
我检查了 id 部分,发现每次刷新时 id 都在变化。 @id='select2-2v7k-container'.
我发现我可以使用下拉菜单的箭头部分。它只工作一次,但对于我的测试用例来说已经足够了。
#select_btn = self.driver.find_element_by_css_selector(".select2-selection__arrow")
select_btn = self.driver.find_element_by_xpath("//span/span[2]")