自主可控,获取网页元素和对网页的操作方法
程序员文章站
2024-03-14 20:29:04
...
//[@id=“auto-id-nLP3VxoUTTZ34fJU”]/div/div/ul/li[1]/a
//[@id=“auto-id”]/div/div/ul/li[1]/a
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.manage().window().maximize(); //最大化页面
driver.close();
driver.submit();
.clear();
.click();
.sendKeys("");
.getText();
driver.findElement(By.cssSelector("[class='']")).click();
Thread.sleep(1000);
Select sel = new Select(driver.findElement(By.id("adtype")));
sel.selectByValue("检修信息");
Select select=new Select();
select.selectByValue("");
select.selectByVisibleText("");
select.selectByIndex(0);
driver.get("");
Thread.sleep(1000);
WebElement = driver.findElement(By.id(""));
.clear();
.sendKeys("");
Thread.sleep(1000);
WebElement = driver.findElement(By.id(""));
.click();
Thread.sleep(1000);
WebElement = driver.findElement(By.xpath(""));
.click();
Thread.sleep(1000);
WebElement = driver.findElement(By.id(""));
Thread.sleep(1000);
WebElement = driver.findElement(By.name(""));
Thread.sleep(1000);
WebElement = driver.findElement(By.linkText(""));定位a标签位置
Thread.sleep(1000);
WebElement = driver.findElement(By.partialLinkText(""));定位a标签中部分文字位置
Thread.sleep(1000);
WebElement = driver.findElement(By.cssSelector(""));定位复制copy selectoer
Thread.sleep(1000);
WebElement = driver.findElement(By.xpath(""));
Thread.sleep(1000);
WebElement = driver.findElement(By.className(""));定位class
Thread.sleep(1000);
WebElement = driver.findElement(By.tagName(""));
Thread.sleep(1000);
.sendKeys(Keys.TAB);
.sendKeys(Keys.PAGE_DOWN);翻页
.sendKeys(Keys.DOWN);
Actions actions = new Actions(driver);
actions.moveToElement( driver.findElement(By.xpath("//*[@id=\"CatList_LinkList_1_Link_0\"]"))).perform();//悬停
context_click()右击
double_click() 双击
drag_and_drop() 拖动
move_to_element() 鼠标悬停在一个元素上
click_and_hold() 按下不放鼠标左键在一个元素
release() 在某个元素上释放鼠标
send_keys(Keys.BACK_SPACE) 删除键(BackSpace)
send_keys(Keys.SPACE) 空格键(Space)
send_keys(Keys.TAB) 制表键(Tab)
send_keys(Keys.ESCAPE) 回退键(Esc)
send_keys(Keys.ENTER) 回车键(Enter)
send_keys(Keys.CONTROL,'a') 全选(Ctrl+A)
send_keys(Keys.CONTROL,'c') 复制(Ctrl+C)
send_keys(Keys.CONTROL,'x') 剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v') 粘贴(Ctrl+V)
WebElement iframe
= driver.findElement(By.className("frame_login"));//定位iframe的
driver.switchTo().frame(iframe);
driver.findElement(By.xpath("//*[@id=\"txt_account\"]")).sendKeys("ycyzharry");//定位之后的网页
driver.findElement(By.id("txt_pwd")).sendKeys("password");
//页面内部的定位
driver.switchTo().defaultContent();//退出来到默认
driver.switchTo().parentFrame();//退出到副本
Set<String> winHandels1 = driver.getWindowHandles(); // 得到当前窗口的set集合
List<String> it1 = new ArrayList<String>(winHandels1); // 将set集合存入list对象
driver.switchTo().window(it1.get(2)); // 切换到弹出的新窗口,更改数字即可切换页面,最前面的为1
Thread.sleep(1000);
//新建一个的网页
//通过 submit() 来提交操作
driver.find_element_by_id("dl_an_submit").submit();
//页面间的切换
String searchHandle = driver.getWindowHandle();//创建一开始的窗口为页柄
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/div[1]/ul/li[2]/a/h3/div")).click();
Thread.sleep(2000);
driver.switchTo().window(searchHandle);//返回原来页面
Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/div[1]/ul/li[3]/a/h3/div")).click();
//警告弹窗处理
import org.openqa.selenium.Alert;//加这个方法
Alert A = driver.switchTo().alert();
A.sendKeys("This is learning Selenium");
A.accept();//确定
A.dismiss();//取消
//上传图片……
driver.findElement(By.xpath("//*[@id=\"form\"]/div/div[2]/div[2]/input")).sendKeys("D:\\照片\\IMG_20190220_162258.jpg");
Thread.sleep(5000);
//下滑页面
import org.openqa.selenium.JavascriptExecutor;//方法
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, 2200)");
Thread.sleep(6000);
//动态id的定位;
xpath("//div[contains(@id, 'auto-id')]");
xpath("//div[contains(@id, 'auto-id')]/div/div/ul/li[1]/a");//改一下之后就可以用了
//选择时间框,是input的标签,但是如果其中有readonly就无法手动输入,只能进行点击;
1.直接输入:
C.clear();
C.sendKeys("2019-10-01");
2.用js:
//import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
removeAttribute.executeScript("var setDate=document.getElementById(\"BeginTime\");setDate.removeAttribute('readonly');") ;
3.直接定位位置xpath
//退出页面
driver.navigate().refresh();
driver.close();
driver.quit();
# 先定位输入框输入关键字
driver.find_element_by_id('id').send_keys('ab')
# 然后定位ul
ul = driver.find_element_by_css_selector(".ui-autocomplete-items")
# 最后定位里面所有值
li = ul.find_elements_by_tag_name('li')
# 选取想要的值
li[0].click() # 0代表选择第一个值
推荐阅读