JavaScript 彈出框處理
js彈出框的方法使用:
先使用switch_to.alert切換到瀏覽器彈出框
再使用alert類(lèi)的操作方法:
dismiss():取消
accept():確定
text():獲取彈框中的文本信息
send_keys():往彈框中輸入文本信息
js之:Alerts彈框:
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.CHROME
desired_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_time
print(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.alert
text_info = alert.text
print(text_info)
alert.accept()
time.sleep(10)
driver.quit()
控制臺(tái)顯示了js彈框中的文本信息:
F:virtualEnvironmentvenvScriptspython.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/element_localization.py
17.46286106109619
Sample alert
Process finished with exit code 0
js之:Confirm彈框:
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.CHROME
desired_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_time
print(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.alert
text_info = alert.text
print(text_info)
alert.dismiss()
time.sleep(10)
driver.quit()
控制臺(tái)顯示了js彈框中的文本信息:
F:virtualEnvironmentvenvScriptspython.exe F:/git/AuomationTest/AuomationTestProject/webTestAuomation/element_localization.py
31.936458826065063
Are you sure?
Process finished with exit code 0
js之:Prompt彈框:
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.CHROME
desired_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.alert
alert.send_keys("自動(dòng)化測(cè)試")
time.sleep(2)
alert.accept()
注意:敲黑板并劃重點(diǎn):
alert.send_key()輸入的數(shù)據(jù),是不會(huì)把彈框中的默認(rèn)參數(shù)替換掉的
所以百度了一個(gè)能看到alert彈框返回值的栗子:
首先我在send_key()中輸入的是 ” 自動(dòng)化測(cè)試 ” 然后點(diǎn)擊 ” 確定 ” ,頁(yè)面顯示的就是我輸入的返回值:
以上總結(jié)或許能幫助到你,或許幫助不到你,但還是希望能幫助到你,如有疑問(wèn)、歧義,評(píng)論區(qū)留言會(huì)及時(shí)修正發(fā)布,謝謝!
本文由網(wǎng)上采集發(fā)布,不代表我們立場(chǎng),轉(zhuǎn)載聯(lián)系作者并注明出處:http://zltfw.cn/shbk/39376.html