本文介绍了从下拉列表中选择时未找到选项元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使在提供正确的XPath之后也无法单击下拉列表。请找到下面的代码供您参考。这里我使用的是选择类。
BasePage:
def SelectSumInsured(self,sumvalue):
sumdropdown = Select(self.driver.find_element(By.XPATH, "//select[@formcontrolname = 'income']"))
suminsured = sumdropdown.select_by_value(sumvalue)
return suminsured
页面对象屏幕:
class HealthInsurancePage(BasePage):
def __init__(self, driver):
super().__init__(driver)
def SelectSum(self,sumvalue):
self.SelectSumInsured(sumvalue)
测试脚本:
现在在测试脚本中,我已经为数据驱动方法使用了PYTEST PARAMETERIZED,它应该根据我在EXCEL表格中提供的数据选择下拉值。
class Test_RSA_Health(BaseTest):
@pytest.mark.parametrize("pin,sumvalue", dataProvider.get_data("excelsheetname"))
def test_RSA_Health(self,pin,sumvalue):
home = HomePage(self.driver)
healthinsuranepage = home.SelectHealth()
self.VerifyPresence_PinCodeTextBox()
healthinsuranepage.landing_page()
healthinsuranepage.InputPin(pin)
healthinsuranepage.SelectSum(str(sumvalue))
错误: 在执行过程中,我看到下拉列表甚至没有被点击,我得到了下面的错误供您参考
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000
但如果我使用使用相同XPath的简单单击方法,则下拉菜单单击成功。
self.driver.find_element(By.XPATH, "//select[@formcontrolname = 'income']").click
我的问题是,为什么不使用Select类单击下拉菜单?
如果需要,可为您的参考提供完整的错误跟踪:
self = <TestCases.test_RSA_Health.Test_RSA_Health object at 0x000002A63A562DF0>, pin = 560008, sumvalue = 700000, mobileno = 9836680067, selfage = 30, fullname = 'Apratim Chaudhuri'
email = 'apratim.chaudhuri@riskcovr.com', firstname = 'Apratim', lastname = 'Chaudhuri', dob = '1/2/1992', income = 1000000, pan = 'AMHPC9725D', designation = 'QA', add1 = 'GD'
add2 = 'SaltLake', height = 180, weight = 75, nomfirstname = 'testnomineefirst', nomlastname = 'testnomineelast', nomdob = '1/2/1992'
@pytest.mark.parametrize("pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob", dataP
rovider.get_data("rsa_health"))
def test_RSA_Health(self,pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob):
home = HomePage(self.driver)
healthinsuranepage = home.SelectHealth()
self.VerifyPresence_PinCodeTextBox()
healthinsuranepage.landing_page()
healthinsuranepage.InputPin(pin)
> healthinsuranepage.SelectSum(str(sumvalue))
test_RSA_Health.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..PagesHealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..PagesBasePage.py:79: in SelectSumInsured
suminsured = sumdropdown.select_by_value(sumvalue)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.support.select.Select object at 0x000002A63A56D7F0>, value = '700000'
def select_by_value(self, value):
"""Select all options that have a value matching the argument. That is, when given "foo" this
would select an option like:
<option value="foo">Bar</option>
:Args:
- value - The value to match against
throws NoSuchElementException If there is no option with specified value in SELECT
"""
css = "option[value =%s]" % self._escapeString(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
> raise NoSuchElementException("Cannot locate option with value: %s" % value)
E selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000
......AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriversupportselect.py:87: NoSuchElementException
---------------------------------------------------------------------------------- Captured log call ----------------------------------------------------------------------------------
INFO Pages.BasePage:BasePage.py:27 Clicking on an Element health_XPATH
INFO Pages.BasePage:BasePage.py:60 Typing in an Element pin_XPATH entered the value as : 560008
_ Test_RSA_Health.test_RSA_Health[560008-500000-9836680067-30-Apratim Chaudhuri-apratim.chaudhuri@riskcovr.com-Apratim-Chaudhuri-1/2/1992-1000000-AMHPC9725D-QA-GD-SaltLake-180-75-testn
omineefirst-testnomineelast-1/2/1992] _
self = <TestCases.test_RSA_Health.Test_RSA_Health object at 0x000002A63A63D4F0>, pin = 560008, sumvalue = 500000, mobileno = 9836680067, selfage = 30, fullname = 'Apratim Chaudhuri'
email = 'apratim.chaudhuri@riskcovr.com', firstname = 'Apratim', lastname = 'Chaudhuri', dob = '1/2/1992', income = 1000000, pan = 'AMHPC9725D', designation = 'QA', add1 = 'GD'
add2 = 'SaltLake', height = 180, weight = 75, nomfirstname = 'testnomineefirst', nomlastname = 'testnomineelast', nomdob = '1/2/1992'
@pytest.mark.parametrize("pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob", dataP
rovider.get_data("rsa_health"))
def test_RSA_Health(self,pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob):
home = HomePage(self.driver)
healthinsuranepage = home.SelectHealth()
self.VerifyPresence_PinCodeTextBox()
healthinsuranepage.landing_page()
healthinsuranepage.InputPin(pin)
> healthinsuranepage.SelectSum(str(sumvalue))
test_RSA_Health.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..PagesHealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..PagesBasePage.py:79: in SelectSumInsured
suminsured = sumdropdown.select_by_value(sumvalue)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.support.select.Select object at 0x000002A63A56DC40>, value = '500000'
def select_by_value(self, value):
"""Select all options that have a value matching the argument. That is, when given "foo" this
would select an option like:
<option value="foo">Bar</option>
:Args:
- value - The value to match against
throws NoSuchElementException If there is no option with specified value in SELECT
"""
css = "option[value =%s]" % self._escapeString(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._setSelected(opt)
if not self.is_multiple:
return
matched = True
if not matched:
> raise NoSuchElementException("Cannot locate option with value: %s" % value)
E selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 500000
......AppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriversupportselect.py:87: NoSuchElementException
根据要求,以下是供您参考的元素
<select _ngcontent-tpo-c7="" formcontrolname="income" class="ng-pristine ng-valid ng-touched">
<!---->
<option _ngcontent-tpo-c7="" class="ng-star-inserted">100000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">200000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">300000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">400000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">500000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">700000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">1000000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">1500000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">2000000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">2500000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">3000000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">4000000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">5000000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">7500000</option>
<option _ngcontent-tpo-c7="" class="ng-star-inserted">10000000</option>
</select>
我编写的XPath是
//select[@formcontrolname = 'income'] `or` `//select[@class='ng-pristine ng-valid ng-touched']`
推荐答案
此错误消息...
raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000
和此错误消息...
raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 500000
...表示html-select元素没有值属性为700000或500000的后代元素。
您需要交叉检查<select>
是否具有以下结构:
<select formcontrolname="income" ...>
<option value="700000" ...>foo</option>
<option value="500000" ...>bar</option>
</select>
结论
正如您提到的看到下拉列表甚至没有被单击,这意味着locator strategies不能唯一标识所需的select
元素。
这篇关于从下拉列表中选择时未找到选项元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!