当前位置: 首页 > news >正文

做手机网站的公司2020给个免费网站好人有好报

做手机网站的公司,2020给个免费网站好人有好报,用vs做网站的教程,连云港 网站 建设写在前面 // 如果文章有问题的地方, 欢迎评论区或者私信指正 目录 什么是Selenium 一个简单的用例 元素定位 id定位 xpath定位 name定位 tag name 定位和class name 定位 操作元素 click send_keys submit text getAttribute 1. 获取元素的 class 属性 2. 获取元素…写在前面 // 如果文章有问题的地方, 欢迎评论区或者私信指正 目录 什么是Selenium 一个简单的用例 元素定位 id定位 xpath定位  name定位 tag name 定位和class name 定位 操作元素 click send_keys submit text getAttribute 1. 获取元素的 class 属性 2. 获取元素的 href 属性对于链接 3. 获取元素的 value 属性对于输入框 4. 获取元素的 style 属性 5. 获取元素的 id 属性 6. 获取元素的自定义数据属性如 data- 属性 添加等待 显示等待 隐式等待  显示等待和隐式等待的特点 显示等待Explicit Waits 隐式等待Implicit Waits 总结 浏览器操作 浏览器前进后退 控制浏览器滚动条 设置浏览器宽、高 键盘事件 键盘按键用法 鼠标事件 定位一组元素  多层框架/窗口定位  处理框架Frame 处理窗口Window 注意事项 下拉框处理 弹窗处理  alert  confirm [待完成] prompt [待完成] 文件上传操作 [待完成] 未雨绸缪  什么是Selenium 在测试领域中Selenium是一个用于自动化测试和浏览器自动化的开源框架。它允许开发人员编写脚本来模拟用户在浏览器中的行为自动执行一系列操作如点击按钮、填写表单、导航到不同页面等。Selenium最初是为Web应用程序的自动化测试而创建的但后来也被广泛用于进行网络数据抓取和网页内容爬取特别是那些需要JavaScript渲染的页面。 Selenium提供了多种编程语言的绑定包括Python、Java、C#、JavaScript等使开发人员能够使用自己熟悉的编程语言来编写自动化脚本。Selenium Grid是Selenium的一个功能它允许同时在多个浏览器和操作系统上运行测试从而提供了更广泛的测试覆盖范围。 一个简单的用例 public class Main {public static void main(String[] args) {ChromeOptions options new ChromeOptions() ;options .addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options) ;webDriver.get(https://www.baidu.com) ;} } 下面来一一解释: ChromeOptions options new ChromeOptions() ; ChromeOptions options new ChromeOptions(); 这行代码是创建了一个新的ChromeOptions对象实例并将其赋值给变量options。ChromeOptions类是Selenium WebDriver中用于配置Chrome浏览器启动选项的一个类。通过ChromeOptions对象你可以设置各种启动参数、扩展程序、用户配置文件等以定制Chrome浏览器的行为。例如你可以设置浏览器窗口的大小、禁用图片加载、启用或禁用JavaScript等。在你创建ChromeOptions对象之后你通常会使用它的方法来添加配置选项。例如你可以使用addArguments方法来添加命令行参数使用setExperimentalOption方法来设置实验性选项或者添加用户数据目录等。例如下面的语句 options .addArguments(--remote-allow-origins*); 具体来说--remote-allow-origins* 这个参数是用来控制哪些远程页面或应用可以与此浏览器实例进行通信的。当设置为*时它允许任何来源的页面或应用与浏览器实例进行通信。 WebDriver webDriver new ChromeDriver(options) ;这行代码是在使用Selenium WebDriver来初始化一个新的Chrome浏览器实例。WebDriver 是Selenium WebDriver库中的一个核心接口, 用于与浏览器进行通信,以自动化网页操作。ChromeDriver: 这是WebDriver接口的一个实现专门用于与Chrome浏览器进行通信。它封装了与Chrome浏览器实例交互所需的所有细节。这行代码创建了一个新的ChromeDriver实例并使用之前配置好的options对象来初始化它。初始化后的ChromeDriver实例赋值给了webDriver变量之后你就可以通过这个变量来控制Chrome浏览器了。 webDriver.get(https://www.baidu.com) ; 打开百度首页。   元素定位 元素的定位是自动化测试的核心想要操作一个对象就必须先拿到他你可以类比一下我们使用js或者jquery来操作html元素你可以通过一个标签的class或者id属性来定位。元素的定位有很多种方法也是通过各种属性进行定位但是无论什么方法都必须保证页面上该属性的唯一性。  webdriver 提供了一系列的对象定位方法常用的有以下几种 idnameclass namelink textpartial link texttag namexpathcss selector 对其定位并获取到这个元素的时候你就可以对其进行各种操作例如点击输入文本等  click 点击对象send_keys 在对象上模拟按键输入clear 清除对象输入的文本内容submit 提交text 用于获取元素的文本信息 操作对象后面讲解 id定位 id属性在我们页面元素中式唯一的在整个页面当中但不是所有的元素都有id有的元素只有class属性所以的一般具有id元素的可以使用id来定位 input typetext classs_ipt namewd idkw maxlength100 autocompleteoff package com.example.forumspringboot27;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions;public class Main {public static void main(String[] args) {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);// 找到百度搜索输入框// 通过CSS选择器WebElement baidu_searcch webDriver.findElement(By.cssSelector(.s_ipt));// 输入信息baidu_searcch.sendKeys(软件测试);} }然后就会自动打开百度的页面然后再对应的 .s_ipt 对应的标签中输入 软件测试 xpath定位  XPath 是一种在XML 文档中定位元素的语言。因为HTML 可以看做XML 的一种实现所以selenium 用户可是使用这种强大语言在web 应用中定位元素。XPath 扩展了上面id 和name 定位方式提供了很多种可能性。         XPATH的获取可以用chrome的F12开发者模式中Element-右键-copy-copy xpath来获取 基于xml通过xpath WebElement baidu_search_xpath webDriver.findElement(By.xpath(//*[id\kw\])); baidu_search_xpath.sendKeys(软件测试2); 你可能会问什么是xpath XPath全称XML Path Language即XML路径语言是一种在XML文档中查找信息的语言。XPath最初是用来搜寻XML文档的但同样适用于HTML文档的搜索。XPath使用路径表达式来选取XML文档中的节点或节点集并可用于从XML文档中提取信息。XPath是XSLT标准中的重要核心组件并且是W3C标准之一。XPath提供了丰富的标准函数库来处理字符串值、数值、日期和时间比较、节点和QName操作、序列操作、逻辑值等。 XPath的主要作用包括 在XML文档中查找信息XPath用于在XML文档中通过元素和属性进行导航以确定XML文档中某部分的位置并遍历XML文档中的元素和属性。作为XSLT的主要元素XPath在XSLT标准中是一个主要元素必须遵循XPath才能使用XSLT文档。XSLT常用于将XML文档转换为其他格式如HTML。提供标准函数库XPath含有超过100个内建的函数用于处理各种数据类型和操作如字符串值、数值、日期和时间比较、节点和QName处理、序列处理以及逻辑运算等。 XPath路径表达式与常规的电脑文件系统中的表达式非常相似使用这些表达式可以在XML文档树状结构中查找节点。因此XPath在XML数据处理和转换中扮演着重要角色。如需了解更多关于XPath的详细信息建议查阅相关编程书籍或在线教程。 -- 学习xmlhttps://www.w3school.com.cn/xml/https://www.w3school.com.cn/xml/ -- 我们现在讲述的例子中主要存在两种类型的xpath 绝对路径/html/head/title 不常用相对路径 相对路径 索引相对路径 属性值相对路径 通配符相对路径 文本匹配 name定位 如果这个元素有name并且元素的name命名在整个页面是唯一的那么我们可以用name来定位这个元素。用上面百度输入框的例子其中元素的属性name”wd”  input typetext classs_ipt namewd idkw maxlength100 autocompleteoff public static void testByName() {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);webDriver.findElement(By.name(wd)).sendKeys(你好);} tag name 定位和class name 定位 从上面的百度输入框的属性信息中我们看到不单单只有id 和name 两个属性 比如class 和tagname(标签名input 就是一个标签的名字可以通过find_element_by_tag_name(input) 函数来定位。classs_ipt通过find_element_by_class_name(s_ipt)函数定位百度输入框。         在这里要注意的是不是所有的元素用 tag name或者 class name来定位元素首先要保证该元素的这两种属性在页面上是唯一的才能够准确的定位。  虽然方便但是要注意唯一性。 还是以百度的为例子 input typetext classs_ipt namewd idkw maxlength100 autocompleteoff public static void testByName() {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);webDriver.findElement(By.className(s_ipt)).clear();// 这里需要注意下面这种方式要尤其注意有没有重复的tagName // webDriver.findElement(By.tagName(input)).sendKeys(hello);} 这里如何定位不一一讲解可以自行search。。。  操作元素 前面讲到了不少知识都是定位元素定位只是第一步定位之后需要对这个元素进行操作。是鼠标点击还是键盘输入或者清除元素的内容或者提交表单等。这个取决于定位元素需要进行的下一步操作。webdriver 中比较常用的操作对象的方法有下面几个 click 点击对象send_keys 在对象上模拟按键输入clear 清除对象输入的文本内容submit 提交text 用于获取元素的文本信息getAttribute 用于获取webDriver对象的属性值 click 触发鼠标点击事件也就是你拿到一个元素button也好input也好只要执行click操作就相当于鼠标直接去点击例如百度页面有一个搜索框和一个百度一下的按钮 input typesubmit value百度一下 idsu classbtn self-btn bg s_btn input typetext classs_ipt namewd idkw maxlength100 autocompleteoff 我们首先获取这两个元素然后在输入框中输入“软件测试”然后再获取“百度一下”这个元素然后执行click操作 public static void testClick() {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);// 获取输入框WebElement kw webDriver.findElement(By.id(kw));// 输入软件测试kw.sendKeys(软件测试);// 获取百度一下WebElement su webDriver.findElement(By.id(su));// 执行click操作su.click();} 输出 send_keys 也就是键盘事件这里不多赘述 // 获取输入框 WebElement kw webDriver.findElement(By.id(kw)); // 输入软件测试 kw.sendKeys(软件测试); submit submit意思为提交可以看见们刚才百度一下那个按钮是一个input标签type为submit input typesubmit value百度一下 idsu classbtn self-btn bg s_btn 这个时候将click操作变为submit也是可以的 WebElement su webDriver.findElement(By.id(su)); // 执行click操作 // su.click(); su.submit(); text 获取文本信息例如百度的页脚有这么一行对应的html为 div idbottom_layer classs-bottom-layer s-isindex-wrap stylevisibility: visible; width: 100%;div classs-bottom-layer-contentp classlha classtext-color href//home.baidu.com target_blank关于百度/a/pp classlha classtext-color hrefhttp://ir.baidu.com target_blankAbout Baidu/a/pp classlha classtext-color href//www.baidu.com/duty target_blank使用百度前必读/a/pp classlha classtext-color href//help.baidu.com target_blank帮助中心/a/pp classlha classtext-color hrefhttps://e.baidu.com/?refer1271 target_blank企业推广/a/pp classlha classtext-colorhrefhttp://www.beian.gov.cn/portal/registerSystemInfo?recordcode11000002000001target_blank京公网安备11000002000001号/a/pp classlha classtext-color hrefhttps://beian.miit.gov.cn target_blank京ICP证030173号/a/pp classlh styledisplay: inline-block;span classtext-color互联网新闻信息服务许可证11220180008/span/pp classlh styledisplay: inline-block;span classtext-color网络文化经营许可证 京网文〔2023〕1034-029号/span/pp classlh styledisplay: inline-block;a classtext-color href//www.baidu.com/licence/target_blank信息网络传播视听节目许可证 0110516/a/pp classlh styledisplay: none;span classtext-color互联网宗教信息服务许可证编号京20220000043/span/pp classlh styledisplay: none;span classtext-color药品医疗器械网络信息服务备案京网药械信息备字2021第00159号/span/pp classlh styledisplay: none;span classtext-color医疗器械网络交易服务第三方平台备案凭证京网械平台备字2020第00002号/span/pp classlh styledisplay: none;span classtext-color药品网络交易服务第三方平台备案凭证京网药平台备字〔2023〕第000002号/span/pp classlh styledisplay: none;span classtext-color©2024nbsp;Baidunbsp;/span/pdiv classaccessibility-iconspan classc-icon/span/divdiv classopen-content-infospan classc-icon c-color-gray2/spandiv classtip-hover-panel styletop: -118px; right: -12px; display: none;div classrest_info_tipdiv classtip-wrapperp classlh tip-item styledisplay: none;a classtext-color hrefhttp://ir.baidu.comtarget_blankAbout Baidu/a/p/divdiv classtip-wrapperp classlh tip-item styledisplay: none;a classtext-color href//www.baidu.com/dutytarget_blank使用百度前必读/a/pp classlh tip-item styledisplay: none;a classtext-color href//help.baidu.comtarget_blank帮助中心/a/pp classlh tip-item styledisplay: none;a classtext-color hrefhttps://e.baidu.com/?refer1271target_blank企业推广/a/pp classlh tip-item styledisplay: none;a classtext-colorhrefhttp://www.beian.gov.cn/portal/registerSystemInfo?recordcode11000002000001target_blank京公网安备11000002000001号/a/p/divdiv classtip-wrapperp classlh tip-item styledisplay: none;a classtext-color hrefhttps://beian.miit.gov.cntarget_blank京ICP证030173号/a/pp classlh tip-item styledisplay: none;span classtext-color互联网新闻信息服务许可证11220180008/span/p/divdiv classtip-wrapperp classlh tip-item styledisplay: none;span classtext-color网络文化经营许可证 京网文〔2023〕1034-029号/span/p/divdiv classtip-wrapperp classlh tip-item styledisplay: none;a classtext-color href//www.baidu.com/licence/target_blank信息网络传播视听节目许可证 0110516/a/p/divdiv classtip-wrapperp classlh tip-itemspan classtext-color互联网宗教信息服务许可证编号京20220000043/span/p/divdiv classtip-wrapperp classlh tip-itemspan classtext-color药品医疗器械网络信息服务备案京网药械信息备字2021第00159号/span/p/divdiv classtip-wrapperp classlh tip-itemspan classtext-color医疗器械网络交易服务第三方平台备案凭证京网械平台备字2020第00002号/span/p/divdiv classtip-wrapperp classlh tip-itemspan classtext-color药品网络交易服务第三方平台备案凭证京网药平台备字〔2023〕第000002号/span/p/divdiv classtip-wrapperp classlh tip-itemspan classtext-color©2024nbsp;Baidunbsp;/span/p/div/div/div/div/div /div 我们获取顶部id为bottom_layer的标签然后获取text public static void testText() {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);WebElement bottomLayer webDriver.findElement(By.id(bottom_layer));System.out.println(bottomLayer.getText());} 控制台输出 getAttribute getAttribute 是 Selenium WebDriver 提供的一个方法用于获取 HTML 元素的特定属性值。这在自动化测试或网页数据抓取时非常有用。下面是一些使用 getAttribute 的示例操作 1. 获取元素的 class 属性 WebElement element driver.findElement(By.id(someElementId)); String className element.getAttribute(class); System.out.println(Class name: className); 2. 获取元素的 href 属性对于链接 WebElement link driver.findElement(By.linkText(Some Link Text)); String href link.getAttribute(href); System.out.println(Link href: href); 3. 获取元素的 value 属性对于输入框 WebElement inputField driver.findElement(By.id(inputFieldId)); String inputValue inputField.getAttribute(value); System.out.println(Input value: inputValue); 4. 获取元素的 style 属性 WebElement styledElement driver.findElement(By.id(styledElementId)); String style styledElement.getAttribute(style); System.out.println(Style: style); 5. 获取元素的 id 属性 WebElement elementWithID driver.findElement(By.xpath(//div[classsomeClass])); String id elementWithID.getAttribute(id); System.out.println(Element ID: id); 6. 获取元素的自定义数据属性如 data- 属性 WebElement customElement driver.findElement(By.id(customElementId)); String dataAttribute customElement.getAttribute(data-custom-attribute); System.out.println(Custom data attribute: dataAttribute); 在使用 getAttribute 方法时你需要知道你想要获取的属性的名称并将其作为字符串参数传递给该方法。这将返回该属性的当前值。如果元素不存在或属性不存在Selenium 通常不会抛出异常而是返回 null 或空字符串。因此在使用返回的属性值之前进行非空检查通常是一个好习惯。 注意在使用 getAttribute 之前确保你已经找到了正确的元素并且该元素确实包含了你想要获取的属性。否则你可能会得到 null 或不正确的值。 添加等待 等待通常用于确保页面元素在尝试与之交互之前已完全加载和可用。Selenium提供了几种等待策略包括显式等待和隐式等待。  显示等待 显示等待可以设置一个等待条件, 具体就是使用WebDriverWait的对象的until方法来设置等待对象, 例如下面的代码案例中, 为这个id为myElementId的元素设置等待条件:最多等待10s. 在使用Selenium的显示等待Explicit Waits时如果设定的超时时间到达而期望的条件仍未满足那么会抛出TimeoutException异常。显示等待允许你明确指定一个等待条件和一个最长等待时间。在等待期间Selenium会定期检查指定的条件是否成立。如果条件在超时之前成立则等待结束并继续执行后续代码如果超时时间到达而条件仍未成立则会抛出异常  import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; // ... 其他代码 ... WebDriverWait wait new WebDriverWait(driver, Duration.ofSeconds(10)); // 等待最多10秒 WebElement element wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(myElementId))); 在上面的代码中WebDriverWait对象被设置为最多等待10秒。然后它使用until方法和ExpectedConditions.visibilityOfElementLocated来等待具有特定ID的元素变得可见。 隐式等待  隐式等待Implicit Waits在Selenium中用于设置WebDriver在查找元素时应该等待的最长时间。如果在设定的时间内元素没有被找到WebDriver将抛出一个NoSuchElementException。隐式等待对WebDriver实例的生命周期内的所有元素查找操作都有效。  import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.concurrent.TimeUnit; public class ImplicitWaitExample { public static void main(String[] args) { // 设置系统属性以指向ChromeDriver的位置 System.setProperty(webdriver.chrome.driver, path/to/chromedriver); // 创建WebDriver实例 WebDriver driver new ChromeDriver(); try { // 打开网页 driver.get(http://example.com); // 设置隐式等待为10秒 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 查找元素WebDriver将等待最多10秒直到元素出现 // 如果在10秒内元素没有出现将抛出NoSuchElementException WebElement element driver.findElement(By.id(someElementId)); // 对元素进行其他操作... } finally { // 关闭浏览器 driver.quit(); } } } 显示等待和隐式等待的特点 显示等待Explicit Waits和隐式等待Implicit Waits在Selenium中都是用来处理页面元素加载和可用性的重要策略但它们之间存在一些关键的区别 显示等待Explicit Waits 特点 精确性显示等待允许你针对特定的条件或元素进行等待确保在继续执行代码之前这些条件或元素已经满足。灵活性你可以为显示等待定义多种条件如元素可见性、可点击性、文本内容等。作用域显示等待只作用于指定的代码块或元素查找操作不会影响其他操作。性能只会在需要的时候等待不会造成不必要的延迟。 使用场景 当你需要等待某个特定元素或条件成立时。当你需要更精确的控制等待时间时。 隐式等待Implicit Waits 特点 全局性隐式等待设置后它将应用于WebDriver实例的所有元素查找操作。简单性隐式等待的设置相对简单只需设置一次即可。限制隐式等待只针对元素查找操作它不会等待元素变为可交互状态如可点击。性能影响隐式等待可能导致不必要的等待时间因为它会在每次元素查找时都生效。 使用场景 当你不需要特别精确的控制只需要确保元素在查找时存在即可。当你不希望为每个元素查找操作都单独设置等待时。 总结 显示等待和隐式等待各有其优缺点适用于不同的场景。显示等待提供了更高的灵活性和精确性但设置相对复杂而隐式等待设置简单且全局生效但可能导致不必要的性能损耗。在实际使用中建议根据具体需求选择合适的等待策略。在大多数情况下显示等待由于其精确性和灵活性更受测试开发人员的青睐。 浏览器操作 浏览器前进后退 public static void testBrowerOperation() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);// 进入百度首页webDriver.get(https://www.baidu.com);WebElement element webDriver.findElement(By.cssSelector(#kw));// 输入框输入 521element.sendKeys(521);WebElement element1 webDriver.findElement(By.cssSelector(#su));Thread.sleep(3000);// 点击搜索element1.click();Thread.sleep(3000);// 浏览器后退webDriver.navigate().back();Thread.sleep(3000);// 刷新浏览器webDriver.navigate().refresh();Thread.sleep(3000);// 浏览器前进webDriver.navigate().forward();} 控制浏览器滚动条 也就是 控制浏览器最右端的滚动条 ...  public static void scroll() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);WebElement element webDriver.findElement(By.cssSelector(#kw));// 输入框输入 521element.sendKeys(521);WebElement element1 webDriver.findElement(By.cssSelector(#su));Thread.sleep(3000);// 点击搜索element1.click(); Thread.sleep(3000);((JavascriptExecutor)webDriver).executeScript(document.documentElement.scrollTop10000);} 这里的top指的是往下滑动, 当然也有向右, 或者向上,等等. 设置浏览器宽、高 全屏操作: public static void setSize() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);WebElement element webDriver.findElement(By.cssSelector(#kw));// 输入框输入 521element.sendKeys(521);WebElement element1 webDriver.findElement(By.cssSelector(#su));Thread.sleep(3000);// 点击搜索element1.click();// 窗口最大化Thread.sleep(3000);webDriver.manage().window().maximize();// 全屏Thread.sleep(3000);webDriver.manage().window().fullscreen();// 自定义窗口大小Thread.sleep(3000);webDriver.manage().window().setSize(new Dimension(800,500));} 键盘事件 键盘按键用法 使用键盘的组合键 public static void testKeyBoard() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);Thread.sleep(3000);WebElement element webDriver.findElement(By.cssSelector(#kw));// 输入框输入 521element.sendKeys(521);// ctrl A 全选Thread.sleep(3000);element.sendKeys(Keys.LEFT_CONTROL, A);// ctrl X 剪切Thread.sleep(3000);element.sendKeys(Keys.LEFT_CONTROL,x);// ctrl V 粘贴Thread.sleep(3000);element.sendKeys(Keys.LEFT_CONTROL,v);} 上述代码中, Keys用来指定键盘上的一些功能按键: 后面的字母如其名,就是指的字母按键, 不区分大小写. 下面是一些按钮的例子: 通过send_keys()调用按键send_keys(Keys.TAB) # TABsend_keys(Keys.ENTER) # 回车send_keys(Keys.SPACE) #空格键send_keys(Keys.ESCAPE) #回退键Esc  组合键实例: send_keys(Keys.CONTROL,a) #全选CtrlAsend_keys(Keys.CONTROL,c) #复制CtrlCsend_keys(Keys.CONTROL,x) #剪贴CtrlXsend_keys(Keys.CONTROL,v) #粘贴CtrlV 鼠标事件 public static void test09() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(https://www.baidu.com);Thread.sleep(3000);WebElement kw webDriver.findElement(By.id(kw));kw.sendKeys(鲜花);WebElement su webDriver.findElement(By.id(su));su.click();Thread.sleep(3000);// 找到图片按钮WebElement webElement webDriver.findElement(By.cssSelector(#s_tab div a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic));// 鼠标右击Thread.sleep(3000);Actions actions new Actions(webDriver);actions.moveToElement(webElement).contextClick().perform();} 下面说一下上述代码的具体执行流程: 设置ChromeOptions: 创建了一个ChromeOptions对象。使用addArguments(--remote-allow-origins*)设置了一个命令行参数允许所有远程来源的连接。这在某些场景下可能是必要的例如当你想要允许跨域请求时。 初始化WebDriver: 使用上面设置的options创建了一个ChromeDriver实例即一个Chrome浏览器的WebDriver。WebDriver是一个接口允许你编程式地控制浏览器。 打开网页: 使用webDriver.get(https://www.baidu.com)打开百度首页。 等待: Thread.sleep(3000); 使得线程休眠3秒确保网页已经加载完成。这不是最佳实践因为等待时间可能不够或过长。更好的做法是使用Selenium的等待机制如WebDriverWait。 搜索操作: 找到搜索框元素id为kw并输入鲜花。找到搜索按钮元素id为su并执行点击操作。 再次等待: 又是一个3秒的等待。 找到图片按钮: 使用CSS选择器找到图片按钮元素。选择器看起来有些复杂并且可能不稳定因为它依赖于特定的DOM结构和类名。如果百度网站的DOM结构发生变化这个选择器可能就不再有效。 鼠标右击: 使用Actions类来模拟鼠标移动到元素上并右击的操作。 上述代码中, Actions类用来生成用户行为, 所有的用户行为都存储在Actions对象中, 然后通过对应的行为 perform来执行行为 Actions类的一些常用操作: contextClick() 右击doubleClick() 双击dragAndDrop() 拖动moveToElement() 移动..... 等等 选定行为之后, 调用行为的perform方法来执行方法. 定位一组元素  有一个复选框页面: html headmeta http-equivcontent-type contenttext/html;charsetutf-8 /titleCheckbox/title /head body h3checkbox/h3 div classwellform classform-horizontaldiv classcontrol-grouplabel classcontrol-label forc1checkbox1/labeldiv classcontrolsinput typecheckbox idc1 //div/divdiv classcontrol-grouplabel classcontrol-label forc2checkbox2/labeldiv classcontrolsinput typecheckbox idc2 //div/divdiv classcontrol-grouplabel classcontrol-label forc3checkbox3/labeldiv classcontrolsinput typecheckbox idc3 //div/divdiv classcontrol-grouplabel classcontrol-label forr1radio/labeldiv classcontrolsinput typeradio idr1 //div/divdiv classcontrol-grouplabel classcontrol-label forr2radio/labeldiv classcontrolsinput typeradio idr2 //div/div/form /div /body /html 这五个元素都是属于input标签, 但是他们的type不同, 前三个是checkbox, 后面两个是radio,也就是单选. 下面的操作是, 选取前三个checkbox来点击 : public static void page01() {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(http://127.0.0.1:5500/test01.html);// 设置隐式等待webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);ListWebElement input webDriver.findElements(By.cssSelector(input));for (WebElement x : input) {// 如果这个input的type为checkbox就进行点击String type x.getAttribute(type);if (type.equals(checkbox)) {x.click();}}} 测试结果如下: 如果选中的这几个input元素的type类型为checkbox, 就给他选中...  多层框架/窗口定位  处理框架Frame 当需要定位的元素位于某个框架内时首先需要切换到该框架。这可以通过switchTo().frame()方法实现。 WebDriver driver new ChromeDriver(); driver.get(http://example.com); // 切换到框架假设框架的id是myFrame driver.switchTo().frame(myFrame); // 现在可以定位框架内的元素了 WebElement element driver.findElement(By.id(elementId)); 处理完框架内的元素后如果需要与主内容交互可以使用switchTo().defaultContent()方法切换回主内容。 // 切换回主内容 driver.switchTo().defaultContent(); 如果框架内部还有嵌套框架可以多次调用switchTo().frame()来逐层切换。 // 切换到外层框架 driver.switchTo().frame(outerFrame); // 切换到内层框架 driver.switchTo().frame(innerFrame); // 定位元素... 处理窗口Window 获取所有窗口句柄 当打开新的窗口或标签页时可以通过getWindowHandles()方法获取所有窗口的句柄。 // 获取所有窗口句柄 SetString windowHandles driver.getWindowHandles(); 切换到特定窗口  通过窗口句柄可以使用switchTo().window()方法切换到特定窗口。 // 假设我们要切换到第二个窗口 IteratorString iterator windowHandles.iterator(); iterator.next(); // 跳过第一个窗口句柄 String newWindowHandle iterator.next(); driver.switchTo().window(newWindowHandle); 关闭窗口 处理完特定窗口后可以使用close()方法关闭它。 // 关闭当前窗口 driver.close(); 注意事项 确保在测试或爬虫脚本中正确处理框架和窗口的切换避免因为上下文错误导致的定位失败。在处理完框架或窗口后尽量恢复到初始状态以便后续的测试或操作。考虑到Web应用的动态性和不确定性建议添加适当的异常处理机制以应对可能出现的异常情况。 下拉框处理 selenium如何实现下拉框的按钮呢? 下面是一个html页面 : htmlbodyselect idShippingMethod onchangeupdateShipping(options[selectedIndex]); nameShippingMethodoption value12.51UPS Next Day Air $12.51/optionoption value11.61UPS Next Day Air Saver $11.61/optionoption value10.69UPS 3 Day Select $10.69/optionoption value9.03UPS 2nd Day Air $9.03/optionoption value8.34UPS Ground $8.34/optionoption value9.25USPS Priority Mail Insured $9.25/optionoption value7.45USPS Priority Mail $7.45/optionoption value3.20 selectedUSPS First Class $3.20/option/select /body/html selenium代码: public static void page02() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(http://127.0.0.1:5500/test01.html);// 首先得获取下拉框元素WebElement selectElement webDriver.findElement(By.cssSelector(#ShippingMethod));// 构造下拉框对象Select select new Select(selectElement);// 下拉框对象提供了很多的用于操作下拉框的方法select.selectByIndex(0); // select标签从0开始// 当然也可以直接指定value值进行选择Thread.sleep(3000);select.selectByValue(7.45);} 通过构造一个Select对象, 对select元素进行封装, 然后这个Select对象提供了很多操作方法, 如上. 弹窗处理  alert  htmlheadmeta charsetUTF-8title/titlescript typetext/javascriptfunction disp_prompt() {var name prompt(Please enter yourname, )if (name ! null name ! ) {document.write(Hello name !)}}/script /headbodyinput typebutton onclickdisp_prompt() value请点击 / /body/html 输入内容, 然后点击确认: 创建selenium代码操作alert对象 public static void page03() throws InterruptedException {ChromeOptions options new ChromeOptions();options.addArguments(--remote-allow-origins*);WebDriver webDriver new ChromeDriver(options);webDriver.get(http://127.0.0.1:5500/test01.html);// 首先找到这个元素:Thread.sleep(3000);WebElement input webDriver.findElement(By.cssSelector(body input[typebutton]));input.click();// alert取消Thread.sleep(3000);webDriver.switchTo().alert().dismiss();// alert弹窗中输入 大鹌鹑Thread.sleep(3000);input.click();webDriver.switchTo().alert().sendKeys(大鹌鹑);// alert弹窗确认Thread.sleep(3000);webDriver.switchTo().alert().accept();} 输出: confirm [待完成] 待完成  prompt [待完成] 文件上传操作 [待完成]
http://www.zqtcl.cn/news/657105/

相关文章:

  • wordpress是建站工具 还是语言表格制作
  • 北京中国建设银行招聘信息网站店标logo图片免费制作
  • 网站建设分金手指专业二七文章网站是怎么做的
  • 东莞网站设计企业怎么制作手机app及网站
  • 林州做网站下载做蛋糕网站
  • 做网站改版的做实验用哪些国外网站
  • 什么是静态页面网站甜品网站建设方案
  • 做一个网站大概多少钱养生网站源码
  • 淘宝客网站建设分类校园网站开发设计报告
  • 个人网站模板 免费儿童编程培训机构
  • 运动健身型网站开发免费ddns域名注册
  • 专业pc网站建设wordpress 支持php7.1
  • 廊坊网站制作系统虚拟服务器搭建
  • 做网站的优势wordpress百度索引链接
  • 网站哪些功能是PHP做的wordpress 正文宽度
  • wordpress考试主题株洲优化公司
  • 怎么做企业网站建设方案怎样查网站有没有备案
  • 浙江短视频seo优化网站专做童装的网站
  • 印刷包装公司网站模板陕西住房和城乡建设厅网站
  • 成都响应式网站建设公司网站 建设的必要性
  • 江苏省建设局官方网站查询wordpress收到登录错误
  • 个人与企业签订网站开发合同北京个人网站建设
  • 阀门网站设计dede静态网站
  • 做暧暧视频网站在线网站建设项目推文
  • 岳池发展建设集团有限公司门户网站毕设做网站工作量够吗
  • 手机网站客户端设计与实现手机网站 焦点图
  • 网站建设常用的开发语言介绍设计公司官网首页
  • 做网站能拿多少钱视频策划方案怎么写
  • 权威的顺德网站建设dw不会写代码能建立网站吗
  • 做网站美工的前途怎么样企业通讯软件下载