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

如何通过做网站赚钱在哪里创建网站

如何通过做网站赚钱,在哪里创建网站,网站设计的公司运营接单,如皋网站开发选择和操控元素的基本方法 前言 选择元素 选择元素的方法 根据 id属性选择元素 根据class属性选择元素 根据tag名选择元素 通过WebElement对象选择元素 示例 等待界面元素出现 原因 解决 操控元素 点击元素 输入框 获取元素信息 获取元素文本内容 获取…

选择和操控元素的基本方法

前言

选择元素

选择元素的方法

根据 id属性选择元素 

根据class属性选择元素

根据tag名选择元素 

通过WebElement对象选择元素 

示例

等待界面元素出现

原因 

解决 

操控元素 

点击元素

输入框 

获取元素信息

获取元素文本内容 

获取元素属性 

获取整个元素对应的HTML 

获取输入框里面的文字 


前言

参考教程:Python + Selenium Web自动化 2024版 - 自动化测试 爬虫_哔哩哔哩_bilibili

上期文章:Selenium-Java版(环境安装)-CSDN博客 

选择元素

选择元素的方法

1.打开开发者工具,选择元素标签

2.点击左上角小箭头

根据 id属性选择元素 

根据id选择元素是最简单高效的方式 

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/stock1.html");// 根据id选择元素,返回的就是该元素对应的WebElement对象WebElement element = wd.findElement(By.id("kw"));// 通过该 WebElement对象,就可以对页面元素进行操作了// 比如输入字符串到这个输入框里element.sendKeys("通讯\n");// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

根据class属性选择元素

获取所有class属性值为animal的元素对应的WebElement对象,放在一个列表里

        List<WebElement> elements = wd.findElements(By.className("animal"));

这里的findElements方法不能少了最后一个s,否则只能返回第一个元素的对象 

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/sample1.html");// 根据 class name 选择元素,返回的是一个列表// 里面都是class 属性值为 animal的元素对应的 WebElement对象List<WebElement> elements = wd.findElements(By.className("animal"));// 取出列表中的每个 WebElement对象,打印出其text属性的值// text属性就是该 WebElement对象对应的元素在网页中的文本内容for (WebElement element : elements) {System.out.println(element.getText());}// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

输出元素

一个元素可以有多个class类型,用空格分开 

<div class="plant big"><span>土豆</span></div> 

只能指定任意一个class 属性值来选择元素

wd.findElements(By.className("plant"));

wd.findElements(By.className("big"));

而不能

wd.findElements(By.className("animal big"));

根据tag名选择元素 

通过指定参数为By.tagName,选择所有的tag名为span的元素 

List<WebElement> elements = wd.findElements(By.tagName("span"));

运行结果 

通过WebElement对象选择元素 

WebDriver对象选择元素的范围是整个 web页面

WebElement对象选择元素的范围是该元素的内部

示例

在id为container的元素里面找到tag名为span的元素 

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/sample1.html");// 根据ID选择元素,限制选择范围是id为'container'的元素内部WebElement element = wd.findElement(By.id("container"));// 查找id为'container'内部的所有span元素List<WebElement> spans = element.findElements(By.tagName("span"));// 遍历spans列表并打印每个span的文本内容for (WebElement span : spans) {System.out.println(span.getText());}// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

输出结果

等待界面元素出现

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/stock1.html");// 根据ID选择元素WebElement element = wd.findElement(By.id("kw"));// 向该元素发送文本 '通讯' 并按下回车键element.sendKeys("通讯\n");// 返回页面ID为'1'的元素WebElement elementWithId1 = wd.findElement(By.id("1"));// 打印该元素的文字内容System.out.println(elementWithId1.getText());// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

此时打开了网站,并和预期结果一致,查询了“通信”

但程序会报错,没有打印id为1的元素的文字内容 

原因 

代码的执行速度比网站的响应速度快很多,代码在执行如下代码时:

WebElement elementWithId1 = wd.findElement(By.id("1"));

 网站还没有返回查询结果,所以代码执行时找不到id为1的元素,因此报错

解决 

创建WebDriver对象后,加入这行代码 ,调用隐式等待方法

wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

当找不到元素时,不立马报错。

而是每隔半秒钟重新寻找该元素,直到找到为止,

如果超出指定最大等待时长(这里是10秒),才会报错。

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.time.Duration;
import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/stock1.html");// 根据ID选择元素WebElement element = wd.findElement(By.id("kw"));// 向该元素发送文本 '通讯' 并按下回车键element.sendKeys("通讯\n");// 返回页面ID为'1'的元素WebElement elementWithId1 = wd.findElement(By.id("1"));// 打印该元素的文字内容System.out.println(elementWithId1.getText());// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

输出结果

也可以在查询后执行如下这条代码,让程序休息一秒等浏览器访问,再找元素。

Thread.sleep(1000);

操控元素 

点击元素

WebElement element=wd.findElement(By.id("go"));
element.click();

输入框 

网站的初始界面

运行代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;import java.time.Duration;
import java.util.Scanner;public class Main {public static void main(String[] args) {// 创建WebDriver对象WebDriver wd = new EdgeDriver();wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));// 调用get方法打开指定网址wd.get("https://www.byhy.net/cdn2/files/selenium/test3.html");// 查找元素WebElement element = wd.findElement(By.id("input1"));// 清除输入框已有的字符串element.clear();// 输入新字符串element.sendKeys("张三");// 创建Scanner对象等待用户输入Scanner scanner = new Scanner(System.in);System.out.println("等待回车键结束程序");scanner.next();// 关闭浏览器wd.quit();}
}

clear方法可以把输入框中已有内容清除

自动打开网站的界面

获取元素信息

获取元素文本内容 

getText

 // 查找元素
WebElement element = wd.findElement(By.id("animal"));// 打印元素的文本
System.out.println(element.getText());

如果获取文本内容出错,可以试一下这两个方法

        // 获取并打印元素的innerText属性String innerText = element.getAttribute("innerText");System.out.println(innerText);
        // 获取并打印元素的textContent属性String textContent = element.getAttribute("textContent");System.out.println(textContent);

获取元素属性 

getAttribute 

// 查找元素
WebElement element = wd.findElement(By.id("input_name"));// 获取并打印元素的class属性
System.out.println(element.getAttribute("class"));

获取整个元素对应的HTML 

获取整个元素对应的HTML文本内容 

// 获取并打印元素的outerHTML属性
String outerHTML = element.getAttribute("outerHTML");
System.out.println(outerHTML);

获取元素内部的HTML文本内容 

// 获取并打印元素的innerHTML属性
String innerHTML = element.getAttribute("innerHTML");
System.out.println(innerHTML);

获取输入框里面的文字 

获取输入框里面的文字内容不能用getText,要用getArrtibute("value") 

// 查找元素
WebElement element = wd.findElement(By.id("input1"));// 获取并打印输入框中的文本
String value = element.getAttribute("value");
System.out.println(value);

文章转载自:

http://FFR1hHnJ.bqnhh.cn
http://M3xyazul.bqnhh.cn
http://mQzPLCDT.bqnhh.cn
http://9kCWRchQ.bqnhh.cn
http://XOkAuB5K.bqnhh.cn
http://7ZiPcwpK.bqnhh.cn
http://vFMIDMA8.bqnhh.cn
http://bcZhgDyl.bqnhh.cn
http://HYkX7GTu.bqnhh.cn
http://uuDtQyos.bqnhh.cn
http://4WpflaNV.bqnhh.cn
http://KvbWWN3T.bqnhh.cn
http://sUq3Lv5U.bqnhh.cn
http://QzcJXf3q.bqnhh.cn
http://ziNJibhY.bqnhh.cn
http://nEFrNqMM.bqnhh.cn
http://4HwGeFey.bqnhh.cn
http://ttFOvFPI.bqnhh.cn
http://FKuffggi.bqnhh.cn
http://6QFQHw4z.bqnhh.cn
http://prLfehoM.bqnhh.cn
http://efmunHfS.bqnhh.cn
http://eyeouAsZ.bqnhh.cn
http://2T91TC7h.bqnhh.cn
http://V991lGqd.bqnhh.cn
http://v19dDTUb.bqnhh.cn
http://c3OJaQKv.bqnhh.cn
http://rZxCw6GC.bqnhh.cn
http://rTRKpAYe.bqnhh.cn
http://cKKVUdCg.bqnhh.cn
http://www.dtcms.com/wzjs/754784.html

相关文章:

  • 国内简洁网站设计网页设计与应用
  • 北京如何做网站网站页面策划
  • 岳阳网站平台设计系统之家win7纯净版
  • 百盛联合建设集团网站恶意点击竞价时用的什么软件
  • 建立个人博客网站的流程无人在线观看高清视频8
  • 工信部企业网站认证政务网站建设情况汇报
  • 用插件做的炫酷网站邯郸电商设计
  • 一个交易网站开发的成本是多少2024下半年要出的新手机
  • 网站微信收款二维码怎么做2008iis添加网站打不开
  • 扬州建设机械网站网站短信验证码接口怎么做
  • 太原做网站的公司排行wordpress 激活邮件
  • 长沙网站设计的公司江西做网站哪家好
  • dede打包好的网站怎么提取模板网页设计模板素材网站大全
  • 一个网站3个相似域名网站开发三大元素
  • 四省网站建设贷款网站源码html
  • 惠州 网站建设公司太原网页设计公司是销售吗
  • 惠州建设局官方网站ps和dw怎么做网站
  • 西安市沣东新城建设局网站平湖企业网站建设
  • 网站建设的知名品牌域名注册网站有哪些
  • 多少钱立案青岛百度seo排名
  • 网站搭建中企动力最行建设发展公司网站
  • 超全的开源建站系统大全影视广告公司网页设计
  • 一般做网站要多少钱邳州市建设局官方网站
  • 网站建设公司antnw怎么查看网页的源代码
  • 网站开发的人天津建设银行官网站首页
  • 网站建设 面试问题wordpress选定文字标红
  • 怎么做网站然后卖出去怎样建立一个自己的网站免费
  • 网站建设与制作段考试题湘潭大学迎新自助网站
  • 设计坞网站怎么样自己如何建设校园网站
  • 定制网站建设费用百度突然搜不到我的网站