selenium自动化测试之单选下拉列表alert弹窗处理页面刷新(代码片段)

cc905 cc905     2022-12-10     139

关键词:

测试内容

1.百度首页-设置-搜索设置,涉及下拉列表处理和单选、弹窗处理

2.百度首页-设置-高级搜索,涉及页面刷新后的处理、下拉列表

技术图片

 

 

 代码实现

 

firefox_driver = webdriver.Firefox()
    firefox_driver.get("https://www.baidu.com/")
    firefox_driver.implicitly_wait(10)
    set_all = firefox_driver.find_element_by_css_selector("#s-usersetting-top")
    print(set_all.text)
  ActionChains(firefox_driver).move_to_element(set_all).perform()
    time.sleep(1)
    firefox_driver.find_element_by_link_text("搜索设置").click()
    set1 = firefox_driver.find_element_by_css_selector("ul.pftab_hd>li.item:nth-child(1)")  # 搜索设置
    set2 = firefox_driver.find_element_by_css_selector("ul.pftab_hd>li.item:nth-child(2)")  # 高级设置
    firefox_driver.find_element_by_css_selector("#s1_2").click()
    firefox_driver.find_element_by_css_selector(".prefpanelgo").click()  # 保存设置
    alert = firefox_driver.switch_to.alert
    print(alert.text)
    alert.accept()
    time.sleep(2)

    time.sleep(1)
    set_all2 = firefox_driver.find_element_by_css_selector("#s-usersetting-top")  # 页面刷新了,要重新拿这个值,不然会报错
    print(set_all2.text)
    print(firefox_driver)
    input1 = firefox_driver.find_element_by_css_selector("input#kw")
    ActionChains(firefox_driver).move_to_element(input1).perform()
    time.sleep(2)
    ActionChains(firefox_driver).move_to_element(set_all2).perform()  # 注意这里
    time.sleep(1)
    firefox_driver.find_element_by_link_text("高级搜索").click()
    print(firefox_driver.current_window_handle)
    time.sleep(1)
    input1 = firefox_driver.find_element_by_css_selector("#adv_keyword")
    input1.clear()
    input1.send_keys("测试")
    time_set_all = firefox_driver.find_element_by_css_selector(".adv-gpc-select > div:nth-child(1) > span:nth-child(1)")
    time_set_all.click()
    firefox_driver.find_element_by_css_selector(".adv-gpc-select > div:nth-child(2) > div:nth-child(2) > p:nth-child(3)").click()
    firefox_driver.find_element_by_css_selector("div.bottom-btn-wrap>input.advanced-search-btn").click()  # 提交
    time.sleep(2)
    result = firefox_driver.title
    print(result)
    time.sleep(3)
    firefox_driver.quit()

分析

1.关于下拉列表

观察可以看到,下拉列表这个div的dispalyed属性值是none,代表其是不可见的,所以这里要先移动到[设置],使其可见,然后再定位下面的选项内容。

2.关于页面刷新

更改搜索设置后,发现页面被刷新了,之前找到的element都不可用了,故这里进行了重新定位

3.关于ActionChains的move_to_elemnet(el)不起作用

页面刷新后,可以重新定位到[设置],并且跟之前的[设置]是同一个元素,但是move_to_el不起效,定位不到下拉菜单的内容。

a. https://bugs.chromium.org/p/chromedriver/issues/detail?id=605里提到,chromeDriver有个已知问题,当光标在浏览器窗口内的时候,这个鼠标事件会不生效;也有实测说当物理鼠标也在移动时,产生的事件会导致页面停止显示下拉菜单。

b. https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/6270 firefoxDriver上也有类似的问题。

解决办法:

根据链接a上网友的评论猜测,是不是前一次执行结束,鼠标其实还停留在[设置]这个元素上,所以再次移动过去,没有任何鼠标停留的结果?

在move_to_el(设置)前,把鼠标先移动到其他元素上,再移动回来,发现成功了。

ActionChains(firefox_driver).move_to_element(input1).perform()
time.sleep(1)
ActionChains(firefox_driver).move_to_element(set_all2).perform()  # 注意这里

selenium自动化测试面试题(代码片段)

1、什么是自动化测试、自动化测试的优势是什么?2、什么样的项目比较适合做自动化测试,什么样的不适合做自动化测试?3、说一下开展自动化工作的主要流程是怎样的?4、在制定自动化测试计划的时候一般要考虑哪些点?5... 查看详情

java+selenium+new——操作单选下拉列表——打印每一个选项——3种方法选中某一个选项——select类(代码片段)

             packagerjcs;importjava.util.*;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.firefox.FirefoxDriver;importo 查看详情

selenium自动化测试入门alert/confirm/prompt弹出窗口处理(代码片段)

一、Alert/Confirm/Prompt弹出窗口特征说明Alert弹出窗口:提示用户信息只有确认按钮,无法通过页面元素定位,不关闭窗口无法在页面上做其他操作。Confirm弹出窗口:有确认和取消按钮,该弹出窗口无法用页面元素定位,不关闭窗... 查看详情

web自动化测试—selenium游览器下拉框操作(代码片段)

#coding=utf-8‘‘‘下拉框实战思路导包:fromselenium.webdriver.support.selectimportSelect#下拉框selectfromselenium.webdriver.common.action_chainsimportActionChains#鼠标操作先定位到下拉框-->>实例化Select类-->>实例化后调用select 查看详情

使用selenium框架在做web自动化测试时,如何处理弹出框?

参考技术AWeb自动化测试中处理弹出框的相关方法(python语言):alert=driver.switch_to.alert#获取弹出框对象alert.text#获取弹出框的提示内容alert.accept()#点击确定按钮,关闭弹出框全套的课程可以找传智播客的,很多大牛的老师讲的都很... 查看详情

使用 selenium 的下拉选择自动化

】使用selenium的下拉选择自动化【英文标题】:Dropdownselectionautomationusingselenium【发布时间】:2013-03-3119:03:11【问题描述】:我需要自动选择selenium中的下拉框。下面给出了包含下拉列表的html代码。每个组件的html代码都是使用fireb... 查看详情

尝试从 Selenium Web 自动化的下拉列表中选择一个选项 -error-“ElementNotInteractableException:无法滚动到视图中”

】尝试从SeleniumWeb自动化的下拉列表中选择一个选项-error-“ElementNotInteractableException:无法滚动到视图中”【英文标题】:TryingtoselectanoptionfromthedropdowninSeleniumwebautomation-error-"ElementNotInteractableException:couldnotbescrolledintoview&qu 查看详情

web自动化测试10:selenium下拉选择框弹出框滚动条操作(代码片段)

...全套学习路线图均在专栏,↑↑戳进去领取~👼Web自动化测试01:认识web自动化在什么项目中适用👼Web自动化测试02:Web自动化测试工具选择大全👼Web自动化测试03:Selenium安装配置,详细教程👼... 查看详情

如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项

...】:2021-04-1915:22:51【问题描述】:我正在使用Selenium开始自动化,这是我第一次单独做,我在这里找不到下拉列表的正确代码https://demoqa.com/automation-p 查看详情

python+selenium自动化测试框架--第一个自动化测试脚本(代码片段)

一、目标打开Chrome浏览器,访问百度网站,搜索selenium,检查搜索列表是否存在selenium官网链接 二、分析1.首先打开浏览器2.访问百度网站3.定位百度搜索输入框4.输入selenium5.点击“百度一下6.在搜索列表中判断是否存在selen... 查看详情

python+selenium自动化测试定位下拉菜单并点击??

请问怎么写下图中的菜单定位并点击的脚本;那就改用点击来解决.先点击倒三角按钮,然后点击optionvalue="132"另外select标签怎么有个editable=false?参考技术A请参考selenium.webdriver.support.select的API.select_by_visible_text(text)[source]Selectalloption... 查看详情

吾八哥学selenium:操作复选框checkbox/单选框radio的方法

...选框radio是web网站里经常会使用到的两个控件,那么在web自动化测试的时候如何利用Selenium来操作这俩控件呢?今天我们就来简单入门练习一下!html测试页面代码如下:<html><head><metahttp-equiv="content-type"content="text/html;cha... 查看详情

selenium测试环境搭建

1、浏览器FirefoxSetup35.0.1   安装完成后设置菜单栏,在上边缘空白处右键,从下拉列表中找到菜单栏。   关闭自动更新选项-高级设置-不检查更新2、插件配置   FireBug调试所有的网站语言  &... 查看详情

在 selenium 中单击 select2 下拉列表时出现问题

】在selenium中单击select2下拉列表时出现问题【英文标题】:Problemwithclickingselect2dropdownlistinselenium【发布时间】:2019-12-2310:57:00【问题描述】:我使用selenium和python编写测试。下拉列表有问题。我尝试使用直接点击和javascriptexecutesc... 查看详情

python3+selenium自动化测试-下拉选择框13

参考技术A(13)下拉选择框selenium的下拉选择框。我们通常会遇到两种下拉框,一种使用的是html的标签select,另一种是使用input标签做的假下拉框。标签select:需要用到Select类,先要导入select方法,fromselenium.webdriver.support.selectimp... 查看详情

当使用 Selenium 和 Python 传递值时,动态下拉列表不会在 https://www.nseindia.com/ 上填充自动建议

】当使用Selenium和Python传递值时,动态下拉列表不会在https://www.nseindia.com/上填充自动建议【英文标题】:Dynamicdropdowndoesn\'tpopulatewithautosuggestionsonhttps://www.nseindia.com/whenvaluesarepassedusingSeleniumandPython【发布时间】:2020-10-0822:19:42【... 查看详情

吾八哥学selenium:操作下拉框select标签的方法

我们在做web页面自动化测试的时候会经常遇到<select></select>标签的下拉框,那么在Python里如何实现去操作这种控件呢?今天就给大家分享一下这个玩法。为了让大家学习更方便,我准备了一个测试页面。测试的html页面代... 查看详情

selenium自动化——alert/confirm/prompt处理

webdriver中处理JavaScript所生成的alert、confirm以及prompt是很简单的。具体思路是使用switch_to.alert()方法定位到alert/confirm/prompt。然后使用text/accept/dismiss/send_keys按需进行操做。text返回alert/confirm/prompt中的文字信息。accept点击确认按钮... 查看详情