js弹窗处理

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)


# 访问网址
driver.get('https://www.w3school.com.cn/tiy/t.asp?f=eg_js_prompt')
driver.maximize_window()  # 浏览器最大化
driver.implicitly_wait(10)  # 单位是秒
driver.switch_to.frame('iframeResult')  # 切换iframe

driver.find_element(By.XPATH, '//button[text()="试一试"]').click()
# 上一步点击后页面上会出现js弹框
# 对于这种弹框我们一定要处理
# alert = driver.switch_to.alert

# 有的网站弹框出现的会慢一点,但时间不确定。直接切换可能会报错
# 所以我们可以使用显式等待来处理
wait = WebDriverWait(driver=driver, timeout=5)
alert = wait.until(expected_conditions.alert_is_present())
text = alert.text  # 得到弹框上的文字
print(text)
alert.accept()  # 确定、接受、同意、是
# alert.dismiss() # 取消、否、不同意
# alert.send_keys('沙陌') # 在有输入框的弹框上输入内容
time.sleep(5)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤实现一个基于PHP的登录注册JS弹窗: 1. 创建一个登录注册页面,包括表单和对应的处理PHP代码。 2. 在登录注册页面中添加一个JavaScript函数,用于显示弹窗。 3. 在JavaScript函数中,使用ajax发送请求来获取登录或注册页面的内容。 4. 将获取到的内容添加到弹窗中。 5. 显示弹窗。 下面是一个简单的示例: HTML代码: ``` <!-- 登录注册表单 --> <form id="loginForm" action="login.php" method="post"> <input type="text" name="username" placeholder="用户名"> <input type="password" name="password" placeholder="密码"> <button type="submit">登录</button> </form> <form id="registerForm" action="register.php" method="post"> <input type="text" name="username" placeholder="用户名"> <input type="password" name="password" placeholder="密码"> <input type="password" name="confirm_password" placeholder="确认密码"> <button type="submit">注册</button> </form> <!-- 登录注册弹窗 --> <div id="loginPopup" class="popup"> <div class="popup-content"></div> </div> <div id="registerPopup" class="popup"> <div class="popup-content"></div> </div> ``` CSS代码: ``` /* 弹窗样式 */ .popup { display: none; position: fixed; z-index: 999; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .popup-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border-radius: 5px; } ``` JavaScript代码: ``` function showLoginForm() { // 显示登录弹窗 var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("loginPopup").style.display = "block"; document.getElementById("loginPopup").innerHTML = this.responseText; } }; xhr.open("GET", "login_form.php", true); xhr.send(); } function showRegisterForm() { // 显示注册弹窗 var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("registerPopup").style.display = "block"; document.getElementById("registerPopup").innerHTML = this.responseText; } }; xhr.open("GET", "register_form.php", true); xhr.send(); } ``` 在上面的代码中,我们使用了两个JavaScript函数showLoginForm()和showRegisterForm(),分别用于显示登录和注册弹窗。这两个函数使用了ajax来异步获取登录和注册页面的内容,并将其添加到对应的弹窗中。 最后,我们可以在登录注册按钮上添加onclick事件来触发弹窗的显示: ``` <button onclick="showLoginForm()">登录</button> <button onclick="showRegisterForm()">注册</button> ``` 这样,当用户点击登录或注册按钮时,就会弹出对应的弹窗,显示登录或注册表单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值