标签:from imp color 怎么 end expected out nal 输入
Java | Python | Ruby | ||
多表单切换 | 指定表单 | switchTo().frame(id/name/定位元素) | switch_to.frame(id/name/定位元素) | switch_to.frame(id/name/定位元素) |
上一级表单 | switchTo().parentFrame() | switch_to.parent_content() | switch_to.parent_frame | |
最外层页面 | switchTo().defaultContent() | switch_to.default_content() | switch_to.default_content | |
多窗体切换 | 指定窗体 | switchTo().window(指定窗体) | switch_to.window(指定窗体) | switch_to.window(指定窗体) |
获取当前窗体句柄 | getWindowHandle() | current_window_handle | window_handle | |
获取所有窗体句柄 | getWindowHandles() | window_handles | window_handles | |
警告框 | 定位警告框 | switchTo().alert() - getText():返回文字信息 - accept():接受现有警告框 - dismiss():解散现有警告框 - sendKeys():发送文本至警告框 |
switch_to_alert() - text:返回文字信息 - accept():接受现有警告框 - dismiss():解散现有警告框 - send_keys():发送文本至警告框 |
switch_to.alert - text:返回文字信息 - accept:接受现有警告框 - dismiss:解散现有警告框 - send_keys():发送文本至警告框 |
当一个页面存在frame/iframe表单嵌套时,WebDriver却只能在一个页面上对元素识别定位,但是对于表单上的嵌套元素无法直接定位,这时候该怎么办呢?
1 package JavaTest; 2 3 import java.util.NoSuchElementException; 4 import java.util.concurrent.TimeUnit; 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.Keys; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 import org.openqa.selenium.interactions.Actions; 10 import org.openqa.selenium.support.ui.ExpectedConditions; 11 import org.openqa.selenium.support.ui.WebDriverWait; 12 13 public class Test { 14 public static void main(String[] arg) throws InterruptedException 15 { 16 WebDriver driver = new FirefoxDriver(); 17 18 // 设置隐示等待时长:10秒; 19 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 20 driver.get("file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/iframe.html"); 21 22 try { 23 // 切换到id为frame1的frame 24 driver.switchTo().frame("frame1"); 25 26 // 接下来获取frame1中百度元素 27 driver.findElement(By.id("kw")).sendKeys("Java"); 28 driver.findElement(By.id("su")).click(); 29 30 //跳回最外层页面 31 driver.switchTo().defaultContent(); 32 33 // 通过元素定位frame2 34 driver.switchTo().frame(driver.findElement(By.id("frame2"))); 35 driver.findElement(By.id("translateContent")).sendKeys("Java"); 36 } 37 catch(NoSuchElementException e) 38 { 39 System.out.println(e.getMessage()); 40 } 41 finally 42 { 43 driver.close(); 44 } 45 } 46 }
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from time import * 4 5 # 启动Firefox浏览器 6 driver = webdriver.Firefox() 7 8 # 隐式等待10S,打开网址(可直接通过frame的id和name定位) 9 driver.implicitly_wait(10) 10 driver.get("file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/iframe.html") 11 12 try: 13 # 切换到id为frame1的frame 14 driver.switch_to.frame("frame1") 15 16 # 接下来获取frame1中百度元素 17 driver.find_element(By.ID, ‘kw‘).send_keys(‘Python‘) 18 driver.find_element(By.ID, ‘su‘).click() 19 20 # driver.switch_to.default_content() 跳回最外层页面 21 # 跳出当前一级表单 22 driver.switch_to.parent_frame() 23 24 # 通过元素定位frame2 25 element = driver.find_element(By.ID, ‘frame2‘) 26 driver.switch_to.frame(element) 27 driver.find_element(By.ID, ‘translateContent‘).send_keys(‘Python‘) 28 except Exception as e: 29 print(e.args[0]) 30 finally: 31 driver.close()
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 # 打开firefox并输入网址 6 driver = Selenium::WebDriver.for :firefox 7 8 # 设置隐式等待时间10S 9 driver.manage.timeouts.implicit_wait = 10 10 driver.navigate.to "file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/iframe.html" 11 12 begin 13 # 切换到id为frame1的frame 14 driver.switch_to.frame(‘frame1‘) 15 16 # 接下来获取frame1中百度元素 17 driver.find_element(:id => ‘kw‘).send_keys(‘Ruby‘) 18 driver.find_element(:id => ‘su‘).click 19 20 # 跳出当前一级表单 21 driver.switch_to.parent_frame 22 23 # 通过元素定位frame2 24 driver.switch_to.frame(driver.find_element(:id => "frame2")) 25 driver.find_element(:id => ‘translateContent‘).send_keys(‘Ruby‘) 26 rescue => e 27 puts e.message # 显示报错信息 28 ensure 29 driver.close 30 end 31 end
当我们点击某个链接时,弹出了新的窗体,这时候我们要在不同的窗体上进行操作又该怎么切换呢?
1 package JavaTest; 2 3 import java.util.ArrayList; 4 import java.util.NoSuchElementException; 5 import java.util.Set; 6 import java.util.concurrent.TimeUnit; 7 import org.openqa.selenium.By; 8 import org.openqa.selenium.WebDriver; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 11 public class Test { 12 public static void main(String[] arg) throws InterruptedException 13 { 14 WebDriver driver = new FirefoxDriver(); 15 16 // 设置隐示等待时长:10秒; 17 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 18 driver.get("http://www.baidu.com"); 19 20 try { 21 driver.findElement(By.xpath("//*[@id=‘u1‘]/a[7]")).click(); // 点击百度登录按钮 22 driver.findElement(By.xpath("//*[@id=‘passport-login-pop-dialog‘]/div/div/div/div[4]/a")).click(); // 进入注册页面 23 driver.findElement(By.xpath("//*[@id=‘TANGRAM__PSP_10__footerULoginBtn‘]")).click(); //点击用户登陆 24 driver.findElement(By.xpath("//*[@id=‘TANGRAM__PSP_10__submitWrapper‘]/a[1]")).click(); //进入忘记密码页面 25 26 // 获取当前所有打开的窗口的句柄 27 Set<String> winHandels = driver.getWindowHandles(); 28 ArrayList<String> windows = new ArrayList<String>(winHandels); 29 30 driver.switchTo().window(windows.get(2)); // 切换到注册页面 31 System.out.println(driver.getCurrentUrl()); 32 driver.findElement(By.xpath("//*[@id=‘TANGRAM__PSP_3__userName‘]")).sendKeys("1234567890"); //输入设置用户名 33 driver.close(); //关闭注册页面 34 driver.switchTo().window(windows.get(0)); //切换到主页 35 driver.close(); //关闭主页 36 driver.switchTo().window(windows.get(1));//切换到忘记密码页面 37 } 38 catch(NoSuchElementException e) 39 { 40 System.out.println(e.getMessage()); 41 } 42 finally 43 { 44 driver.close(); 45 } 46 } 47 }
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 4 # 启动Firefox浏览器 5 driver = webdriver.Firefox() 6 7 # 隐式等待10S,打开网址(可直接通过frame的id和name定位) 8 driver.implicitly_wait(10) 9 driver.get("http://www.baidu.com") 10 11 try: 12 driver.find_element(By.XPATH, "//*[@id=‘u1‘]/a[7]").click() # 点击百度登录按钮 13 driver.find_element(By.XPATH, "//*[@id=‘passport-login-pop-dialog‘]/div/div/div/div[4]/a").click() # 进入注册页面 14 driver.find_element(By.XPATH, "//*[@id=‘TANGRAM__PSP_10__footerULoginBtn‘]").click() # 点击用户登陆 15 driver.find_element(By.XPATH, "//*[@id=‘TANGRAM__PSP_10__submitWrapper‘]/a[1]").click() # 进入忘记密码页面 16 17 # 获取当前所有打开的窗口的句柄 18 windows = driver.window_handles 19 20 driver.switch_to.window(windows[-1]) # 切换到注册页面 21 driver.find_element(By.XPATH, "//*[@id=‘TANGRAM__PSP_3__userName‘]").send_keys(‘1234567890‘) # 输入设置用户名 22 driver.close() # 关闭注册窗体 23 driver.switch_to.window(windows[0]) # 切换到主页 24 driver.close() # 关闭主页 25 driver.switch_to.window(windows[1]) # 切换到忘记密码页面 26 except Exception as e: 27 print(e.args[0]) 28 finally: 29 driver.close()
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 # 打开firefox并输入网址 6 driver = Selenium::WebDriver.for :firefox 7 8 # 设置隐式等待时间10S 9 driver.manage.timeouts.implicit_wait = 10 10 driver.navigate.to "http://www.baidu.com" 11 12 begin 13 driver.find_element(:xpath => "//*[@id=‘u1‘]/a[7]").click # 点击百度登录按钮 14 driver.find_element(:xpath => "//*[@id=‘passport-login-pop-dialog‘]/div/div/div/div[4]/a").click # 进入注册页面 15 driver.find_element(:xpath => "//*[@id=‘TANGRAM__PSP_10__footerULoginBtn‘]").click # 点击用户登陆 16 driver.find_element(:xpath => "//*[@id=‘TANGRAM__PSP_10__submitWrapper‘]/a[1]").click # 进入忘记密码页面 17 18 # 获取当前所有打开的窗口的句柄 19 windows = driver.window_handles 20 21 driver.switch_to.window(windows[-1]) # 切换到注册页面(2/-1) 22 driver.find_element(:xpath => "//*[@id=‘TANGRAM__PSP_3__userName‘]").send_keys(‘1234567890‘) # 输入设置用户名 23 driver.close # 关闭注册窗体 24 driver.switch_to.window(windows[0]) # 切换到注册页面 25 driver.close # 关闭主页 26 driver.switch_to.window(windows[1]) # 切换到忘记密码页面 27 rescue => e 28 puts e.message # 显示报错信息 29 ensure 30 driver.close 31 end 32 end
为什么要把警告框放在这里呢?我们把页面想象成一个画布,我们在当前的画布层进行操作,弹出的警告框就像是画布的顶层。我们要切换到“画布”顶层将其处理才可进行后续操作。
1 package JavaTest; 2 3 import java.util.NoSuchElementException; 4 import java.util.concurrent.TimeUnit; 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.firefox.FirefoxDriver; 8 9 public class Test { 10 public static void main(String[] arg) throws InterruptedException 11 { 12 WebDriver driver = new FirefoxDriver(); 13 14 // 设置隐示等待时长:10秒; 15 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 16 driver.get("file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/alert.html"); 17 18 try { 19 driver.findElement(By.xpath("//*[@id=‘btn‘]")).click();//点击"点我一下"按钮 20 driver.switchTo().alert().accept();;//接受现有警告框 21 } 22 catch(NoSuchElementException e) 23 { 24 System.out.println(e.getMessage()); 25 } 26 finally 27 { 28 driver.close(); 29 } 30 } 31 }
问题一:
采用代码:driver.switch_to.alert().accept() # 接受现有警告框
报错提示:‘Alert’ object is not callable
解决方法:
对象不能用函数形式调用,就是不能callable。
更换代码:driver.switch_to_alert().accept() # 接受现有警告框
问题二:
try: driver.find_element(By.XPATH, "//*[@id=‘btn‘]").click() # 点击"点我一下"按钮 print(driver.switch_to_alert().text) # 接受现有警告框 except Exception as e: print(e.args[0]) finally: driver.close()
解决方法:
driver.close()用来关闭当前窗体,driver.quit()用来退出相关驱动程序和关闭所有窗口。
目前我们仍然处于alert,close()无法适用。要么先将alert进行接受处理后用close()方法,要么直接采用quit()退出驱动。
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 4 # 启动Firefox浏览器 5 driver = webdriver.Firefox() 6 7 # 隐式等待10S,打开网址(可直接通过frame的id和name定位) 8 driver.implicitly_wait(10) 9 driver.get("file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/alert.html") 10 11 try: 12 driver.find_element(By.XPATH, "//*[@id=‘btn‘]").click() # 点击"点我一下"按钮 13 driver.switch_to_alert().accept() # 接受现有警告框 14 except Exception as e: 15 print(e.args[0]) 16 finally: 17 driver.quit()
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 # 打开firefox并输入网址 6 driver = Selenium::WebDriver.for :firefox 7 8 # 设置隐式等待时间10S 9 driver.manage.timeouts.implicit_wait = 10 10 driver.navigate.to "file:///C:/Users/%E6%9B%B9%E5%B8%85/Desktop/alert.html" 11 12 begin 13 driver.find_element(:xpath => "//*[@id=‘btn‘]").click # 点击"点我一下"按钮 14 puts driver.switch_to.alert.text # 显示警告框文本 15 driver.switch_to.alert.accept # 接受现有警告框 16 rescue => e 17 puts e.message # 显示报错信息 18 ensure 19 driver.close 20 end 21 end
标签:from imp color 怎么 end expected out nal 输入
原文地址:https://www.cnblogs.com/CSgarcia/p/9413909.html