1.selenium常ç¨api
2.å¦ä½ä½¿ç¨PageFactory
3.Python+selenium如何定位多层嵌套元素
selenium常ç¨api
æµè§å¨å®ä½/æä½å¯¹è±¡
webdriver æä¾äºä¸ç³»åçå ç´ å®ä½æ¹æ³ï¼å¸¸ç¨çæ以ä¸å ç§ï¼
è·åå个çï¼find_element_by_xxxï¼ï¼
è·åææçï¼find_elements_by_xxxï¼ï¼è·åææç
ä¼ æ°æ®ï¼send_keysï¼ï¼
ç¹å»ï¼clickï¼ï¼
æ¸ é¤æ°æ®ï¼clearï¼ï¼
æ交ï¼submit()
å ¶ä»å¸¸ç¨WebElementæ¹æ³
è·å该对象ç尺寸大å°ï¼driver.find_element_by_id("kw").size()
è·å该对象çææ¬ï¼driver.find_element_by_id("cp").text
è·å该对象çå±æ§ï¼driver.find_element_by_id('su').get_attribute('type')
该对象æ¯å¦å¯è§ï¼å¸å°ï¼ï¼driver.find_element_by_id("kw").is_displayed()
é®çäºä»¶
from selenium.webdriver.common.keys import Keys
å±çº§å®ä½
driver.find_element_by_id('xx').find_element_by_link_text('xx').click()
è¿éç¨å°äºäºæ¬¡å®ä½ï¼éè¿å¯¹ Link1 çåå»ä¹åï¼åºç°ä¸æèåï¼
å å®ä½å°ä¸æèåï¼åå®ä½ä¸æè
åä¸çé项ãå½ç¶ï¼å¦æèåé项éè¦åå»ï¼å¯éè¿äºæ¬¡å®ä½åä¹ç´æ¥è· click()æä½ã
frameå®ä½
switch_to_frameæ¹æ³
跳转å¥æ
selectï¼optionï¼
对è¿ç§ç±»åçä¸ææ¡ä¸è¬çå¤çæ¯ä¸¤æ¬¡ç¹å»ï¼ç¬¬ä¸ç¹å»å¼¹åºä¸ææ¡ï¼
第äºæ¬¡ç¹å»æä½å ç´ ãå½ç¶ï¼ä¹æäºä¸ææ¡æ¯é¼ æ 移ä¸å»ç´æ¥å¼¹åºçï¼
é£å¯ä»¥ä½¿ç¨ move_to_element()è¿è¡æä½
æ件ä¸ä¼
ç´æ¥å®ä½fileæ ç¾sendkeysç»å¯¹è·¯å¾æè ç¸å¯¹è·¯å¾
è°ç¨js
execute_script
æ§å¶æµè§å¨æ»å¨æ¡
å©ç¨jsæä½æ»å¨æ¡å®ç°ï¼ä½æ¯selenium3ä¸æ¯æèªå¨æ索页é¢ï¼ä¸éè¦åæä½æ»å¨æ¡æ¥æ£ç´¢æ ç¾å ç´ ï¼äºè§£å³å¯
js="var q=document.documentElement.scrollTop="
driver.execute_script(js)
time.sleep(2)
cookieæä½
è·å对象çå±æ§
å¦ä½ä½¿ç¨PageFactory
ä¸ä¸ªç®åçä¾å
为äºä½¿ç¨PageFactoryï¼é¦å éè¦å¨PageObject(页é¢å¯¹è±¡)ä¸å£°æä¸äºå ç´ (WebElements
æ RenderedWebElements)ï¼ä¾å¦ï¼
Java代ç
package org.openqa.selenium.example;
import org.openqa.selenium.WebElement;
public class GoogleSearchPage
{
private WebElement q;
public void searchFor(String text)
{
q.sendKeys(text);
q.submit();
}
}
package org.openqa.selenium.example;
import org.openqa.selenium.WebElement;
public class GoogleSearchPage
{
private WebElement q;
public void searchFor(String text)
{
q.sendKeys(text);
q.submit();
}
}
为äºè®©è¿æ®µä»£ç å·¥ä½ï¼èä¸æ¯å 为âQâæ¯ä¸è½å®ä¾åæåºä¸ä¸ªNullPointerExceptionï¼æ以æ们éè¦åå§åPageObjectï¼
Java代ç
package org.openqa.selenium.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;
public class UsingGoogleSearchPage
{
public static void main(String[] args)
{
WebDriver driver = new HtmlUnitDriver();
driver.get("/");
GoogleSearchPage page = PageFactory.initElements(driver,源码 GoogleSearchPage.class);
page.searchFor("Cheese");
}
}
package org.openqa.selenium.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;
public class UsingGoogleSearchPage
{
public static void main(String[] args)
{
WebDriver driver = new HtmlUnitDriver();
driver.get("/");
GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);
page.searchFor("Cheese");
}
}
Python+selenium如何定位多层嵌套元素
有时候,什么定位元素的源码unityugui源码方法都试过了,还是源码源码天空 官网定位不到元素,就考虑frame切换问题 driver.switchTo().frame("定位到的源码dedeems 标签源码管理frame元素"); //接下来就可以在这个frame框架内定位该框架里的元素了 driver.findElement(By.name("message")).sendKeys("top"); //如果定位另一框架内元素,需返回到web页面,源码python量化交易 源码再进行切换 driver.switchTo().defaultContent(); driver.switchTo().frame("要定位的源码蜜桃影院源码安装frame元素");