从Python Selenium选择AngularJS应用程序中的下拉列表

2024-10-01 13:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想测试PythonSelenium中的页面,它使用AngularJS。它包含一个select选项,我尝试了很多选项来选择它,但是失败了。在

<select id="xtzpp001" ng-model="..." ng-options="...." ng-change="changeView()"> <option value class>select</option> <option value="200" selected="selected">Student 1</option> <option value="201">Student 2</option> <option value="202">Student 3</option> </select>

就像我说的,我试着

^{pr2}$

那么

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('xtzpp001'))
for i in select.options:
   .......etc etc

我也使用显式等待。在

我开始知道pytractor可以在这种情况下帮助我(虽然还没有被积极开发),但是我找不到任何关于使用pytractor选择选项的文档。在

有没有其他方法可以达到这个目的??或pytractor同等产品?在

或者我可以使用量角器(或任何javascript框架)从这个阶段继续在Javascript中进行测试,并在Python中集成/回调结果吗??在


Tags: idvalue选项etc页面ngselectstudent
3条回答

如果您尝试了所有可能的尝试,但始终没有成功,您应该尝试使用execute_script(),如下所示:-

element = driver.find_element_by_id("xtzpp001")

driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Student 2");

使用Select class fromselenium.webdriver.support.ui。将select webelement传递给构造函数。使用selectByVisibleText。参考@alecxe在post-Selenium - Python - drop-down menu option value中的答案

也可以使用操作单击菜单项,下面是一个示例:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

相关问题 更多 >