logo
🛠️ 开发工具

Selenium

Selenium Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 开发工具🧭 Markdown 速查🏷️ 2 个标签
#selenium#testing
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Initialize Browser Drivers
BrowserSyntax
ChromeWebDriver driver = new ChromeDriver();
EdgeWebDriver driver = new EdgeDriver();
FirefoxWebDriver driver = new FirefoxDriver();
SafariWebDriver driver = new SafariDriver();

{.show-header .left-text}

Selenium Locators
LocatorSyntax
classNamedriver.findElement(By.className (“key”))
cssSelectordriver.findElement(By.cssSelector(key"))
id driver.findElement(By.id(key"))
linkTextdriver.findElement(By.linkText(key"))
namedriver.findElement(By.name(“key"))
partialLinkTextdriver.findElement(By.partialLinkText(“key"))
tagName driver.findElement(By.tagName (“key”))
xpathdriver.findElement(By.xpath(“key"))

{.show-header .left-text}

Navigate to URL
SHELL
滚动查看更多
driver.get("https://www.example.com");
Close all browser windows
SHELL
滚动查看更多
driver.quit();
Close the current browser window
SHELL
滚动查看更多
driver.close();
Navigation
DescriptionSyntax
Navigate to a new URLdriver.navigate().to("https://www.newsite.com");
Go forward to the next pagedriver.navigate().forward();
Go back to the previous page driver.navigate().back();
Refresh the current pagedriver.navigate().refresh();

{.show-header .left-text}

Frame Handling
DescriptionSyntax
Switch to a framedriver.switchTo().frame("frameId");
Switch back to the main contentdriver.switchTo().defaultContent();

{.show-header .left-text}

Alerts/Pop-Ups
SHELL
滚动查看更多
Alert alert = driver.switchTo().alert();

// Used to click on the ‘OK’ button of the alert.
alert.accept();

// Used when the ‘Cancel’ button is clicked in the alert box.
alert.dismiss();

// Used to send text to an alert
alert.sendKeys("text");

// Used to capture the alert message.
alert().getText();

Mouse and Keyboard Actions
SHELL
滚动查看更多
Actions actions = new Actions(driver);

// Shifts the mouse pointer to the center of the element and click on it
actions.moveToElement(element).click().build().perform();

// Performs double click on the element
actions.doubleClick(element).perform();

// Drags the element from one point and drops to another
actions.dragAndDrop(source, target).perform();

// Sends a series of keys to the element
actions.sendKeys(element, "text").perform();

Page Information
SHELL
滚动查看更多
// Get the page title
driver.getTitle();

// Get the current page URL
driver.getCurrentUrl();

// Get the entire page source code
driver.getPageSource();

Waits
SHELL
滚动查看更多
// Explicit Wait :- These are conditional waits can be applied to satisfy a particular condition
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
SHELL
滚动查看更多
// Implicit Wait :- Once the command is run, Implicit Wait remains for the entire duration for which the browser is open.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
SHELL
滚动查看更多
// Fluent Wait :- Fluent Wait in Selenium marks the maximum amount of time for Selenium WebDriver to wait for a certain condition (web element) becomes visible. It also defines how frequently WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.
FluentWait wait = new FluentWait(driver);
//Specify the timeout of the wait
wait.withTimeout(5000, TimeUnit.MILLISECONDS);
//Specify polling time
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
//Specify what exceptions to ignore
wait.ignoring(NoSuchElementException.class)
//This is how we specify the condition to wait on.
wait.until(ExpectedConditions.alertIsPresent());

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572