js弹窗编程代码大全(网页html弹窗公告代码)

来源:国外服务器 在您之前已被浏览:1 次
导读:目前正在解读《js弹窗编程代码大全(网页html弹窗公告代码)》的相关信息,《js弹窗编程代码大全(网页html弹窗公告代码)》是由用户自行发布的知识型内容!下面请观看由(国外主机 - www.2bp.net)用户发布《js弹窗编程代码大全(网页html弹窗公告代码)》的详细说明。
国外云主机,www.2 bp .net

JavaScript 弹出框处理


js弹出框的方法使用:

先使用switch_to.alert切换到浏览器弹出框

再使用alert类的操作方法:

  • dismiss():取消
  • accept():确定
  • text():获取弹框中的文本信息
  • send_keys():往弹框中输入文本信息

js之:Alerts弹框:

js弹窗编程代码大全(网页html弹窗公告代码)js弹窗编程代码大全(网页html弹窗公告代码)import timefrom selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdesired_capabilities = DesiredCapabilities.CHROMEdesired_capabilities['pageLoadStrategy'] = 'normal'driver = webdriver.Chrome()start_time = time.time()driver.get('https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/')end_time = time.time() - start_timeprint(end_time)driver.find_element_by_xpath("//a[contains(text(),'See an example alert')]").click()WebDriverWait(driver, 10).until(EC.alert_is_present())alert = driver.switch_to.alerttext_info = alert.textprint(text_info)alert.accept()time.sleep(10)driver.quit()

控制台显示了js弹框中的文本信息:

F:virtualEnvironmentvenvScriptspython.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/element_localization.py17.46286106109619Sample alertProcess finished with exit code 0

js之:Confirm弹框:

js弹窗编程代码大全(网页html弹窗公告代码)js弹窗编程代码大全(网页html弹窗公告代码)import timefrom selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdesired_capabilities = DesiredCapabilities.CHROMEdesired_capabilities['pageLoadStrategy'] = 'normal'driver = webdriver.Chrome()start_time = time.time()driver.get('https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/')end_time = time.time() - start_timeprint(end_time)driver.find_element_by_css_selector( "section:nth-child(2) div.padding.highlightable:nth-child(2) div:nth-child(3) p:nth-child(8) > a.highlight").click()WebDriverWait(driver, 10).until(EC.alert_is_present())alert = driver.switch_to.alerttext_info = alert.textprint(text_info)alert.dismiss()time.sleep(10)driver.quit()

控制台显示了js弹框中的文本信息:

F:virtualEnvironmentvenvScriptspython.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/element_localization.py31.936458826065063Are you sure?Process finished with exit code 0

js之:Prompt弹框:

js弹窗编程代码大全(网页html弹窗公告代码)js弹窗编程代码大全(网页html弹窗公告代码)import timefrom selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdesired_capabilities = DesiredCapabilities.CHROMEdesired_capabilities['pageLoadStrategy'] = 'normal'driver = webdriver.Chrome()start_time = time.time()driver.get('https://www.w3school.com.cn/tiy/t.asp?f=js_prompt')driver.find_element_by_css_selector( "body:nth-child(2) div:nth-child(2) ul:nth-child(1) li:nth-child(4) > a:nth-child(1)").click()iframe = driver.find_element_by_xpath("//iframe[@id='iframeResult']")driver.switch_to.frame(iframe)driver.find_element_by_css_selector("body:nth-child(2) > button:nth-child(2)").click()WebDriverWait(driver, 10).until(EC.alert_is_present())alert = driver.switch_to.alertalert.send_keys("自动化测试")time.sleep(2)alert.accept()

注意:敲黑板并划重点:

alert.send_key()输入的数据,是不会把弹框中的默认参数替换掉的

所以百度了一个能看到alert弹框返回值的栗子:

首先我在send_key()中输入的是 ” 自动化测试 ” 然后点击 ” 确定 ” ,页面显示的就是我输入的返回值:

js弹窗编程代码大全(网页html弹窗公告代码)js弹窗编程代码大全(网页html弹窗公告代码)

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,评论区留言会及时修正发布,谢谢!

国外云主机,www.2 bp .net
提醒:《js弹窗编程代码大全(网页html弹窗公告代码)》最后刷新时间 2023-03-27 02:22:24,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《js弹窗编程代码大全(网页html弹窗公告代码)》该内容的真实性请自行鉴别。