To install Selenium for Python, use pip:
pip install selenium
For Java, add this to your pom.xml (Maven):
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
</dependency>
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com")
driver.get("https://www.example.com");
current_url = driver.current_url
String currentUrl = driver.getCurrentUrl();
title = driver.title
String title = driver.getTitle();
driver.quit()
driver.quit();
element = driver.find_element(By.ID, "myElementId")
WebElement element = driver.findElement(By.id("myElementId"));
element = driver.find_element(By.NAME, "myElementName")
WebElement element = driver.findElement(By.name("myElementName"));
element = driver.find_element(By.CLASS_NAME, "myClassName")
WebElement element = driver.findElement(By.className("myClassName"));
element = driver.find_element(By.CSS_SELECTOR, "#myId > div.class")
WebElement element = driver.findElement(By.cssSelector("#myId > div.class"));
element = driver.find_element(By.XPATH, "//input[@name='username']")
WebElement element = driver.findElement(By.xpath("//input[@name='username']"));
driver.implicitly_wait(10) # seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myElementId"))
)
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myElementId")));
element.click()
element.click();
element.send_keys("Hello, World!")
element.sendKeys("Hello, World!");
element.clear()
element.clear();
element.submit()
element.submit();
import unittest
class MyTest(unittest.TestCase):
def test_title(self):
self.assertEqual(driver.title, "Expected Title")
def test_element_text(self):
element = driver.find_element(By.ID, "myElementId")
self.assertEqual(element.text, "Expected Text")
def test_element_attribute(self):
element = driver.find_element(By.ID, "myElementId")
self.assertEqual(element.get_attribute("class"), "expected-class")
import org.junit.Assert;
public class MyTest {
@Test
public void testTitle() {
Assert.assertEquals("Expected Title", driver.getTitle());
}
@Test
public void testElementText() {
WebElement element = driver.findElement(By.id("myElementId"));
Assert.assertEquals("Expected Text", element.getText());
}
@Test
public void testElementAttribute() {
WebElement element = driver.findElement(By.id("myElementId"));
Assert.assertEquals("expected-class", element.getAttribute("class"));
}
}
alert = driver.switch_to.alert
alert.accept()
Alert alert = driver.switchTo().alert();
alert.accept();
alert = driver.switch_to.alert
alert.dismiss()
Alert alert = driver.switchTo().alert();
alert.dismiss();
alert = driver.switch_to.alert
alert_text = alert.text
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
# By index
driver.switch_to.frame(0)
# By name or ID
driver.switch_to.frame("frameName")
# By WebElement
frame_element = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(frame_element)
// By index
driver.switchTo().frame(0);
// By name or ID
driver.switchTo().frame("frameName");
// By WebElement
WebElement frameElement = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(frameElement);
# Switch to new window
original_window = driver.current_window_handle
for window_handle in driver.window_handles:
if window_handle != original_window:
driver.switch_to.window(window_handle)
break
// Switch to new window
String originalWindow = driver.getWindowHandle();
for (String windowHandle : driver.getWindowHandles()) {
if(!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
break;
}
}
js_code = "return document.title;"
title = driver.execute_script(js_code)
String jsCode = "return document.title;";
String title = (String) ((JavascriptExecutor) driver).executeScript(jsCode);
driver.save_screenshot("screenshot.png")
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(screenshot.toPath(), Paths.get("screenshot.png"));
file_input = driver.find_element(By.ID, "fileInput")
file_input.send_keys("/path/to/file.txt")
WebElement fileInput = driver.findElement(By.id("fileInput"));
fileInput.sendKeys("/path/to/file.txt");
2024 © All rights reserved - buraxta.com