从下拉列表中选择时找不到选项元素
Option element not found while selecting from the dropdown
即使提供了正确的 xpath 也无法单击下拉菜单。请在下面找到代码供您参考。这里我使用 Select class.
底页:
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 参数化数据驱动方法,它应该 select 根据我在 excel sheet 中提供的数据下拉值。
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 class?
单击
完整的错误跟踪供您参考:
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:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..\Pages\BasePage.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
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.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:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..\Pages\BasePage.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
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.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 元素没有具有 value 属性的后代 <option>
元素700000 或 500000.
如果 <select>
具有以下结构,您需要交叉检查:
<select formcontrolname="income" ...>
<option value="700000" ...>foo</option>
<option value="500000" ...>bar</option>
</select>
结论
正如您提到的 看到下拉菜单甚至没有被点击,这意味着 没有识别出所需的 select
元素唯一。
即使提供了正确的 xpath 也无法单击下拉菜单。请在下面找到代码供您参考。这里我使用 Select class.
底页:
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 参数化数据驱动方法,它应该 select 根据我在 excel sheet 中提供的数据下拉值。
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 class?
单击完整的错误跟踪供您参考:
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:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..\Pages\BasePage.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
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.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:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
self.SelectSumInsured(sumvalue)
..\Pages\BasePage.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
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.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 元素没有具有 value 属性的后代 <option>
元素700000 或 500000.
如果 <select>
具有以下结构,您需要交叉检查:
<select formcontrolname="income" ...>
<option value="700000" ...>foo</option>
<option value="500000" ...>bar</option>
</select>
结论
正如您提到的 看到下拉菜单甚至没有被点击,这意味着 select
元素唯一。