[JAVA] Selenium webdriver Summary of frequently used operation methods

Introduction

Hello. This is @mochio from QA. This article is the 20th day of LIFULL Advent Calendar 2017.

We carry out automated tests in our business, and we use Selenium there. Please refer to various articles about Selenium. I made it to organize it because it gets confused because the writing style is a little different between languages.

Launch your browser with a specific URL

Java


driver.get("URL");

C#


driver.Url = "URL";

Python


driver.get("URL")

Ruby


driver.get("URL") 

When you want to change pages

Java


driver.navigate().to("URL");

C#


driver.Navigate().GoToUrl("URL");

Python


driver.get("URL")

Ruby


driver.navigate.to("URL") 

When you want to go back one step

Java


driver.navigate().back();

C#


driver.Navigate().Back();

Python


driver.back()

Ruby


driver.navigate.back 

When you want to move forward

Java


driver.navigate().forward();

C#


driver.Navigate().Forward();

Python


driver.forward()

Ruby


driver.navigate.forward 

Update your browser

Java


driver.navigate().refresh();

C#


driver.Navigate().Refresh();

Python


driver.refresh()

Ruby


driver.navigate.refresh

When you want to know the current URL

Java


driver.getCurrentUrl()

C#


driver.Url;

Python


driver.current_url

Ruby


driver.current_url

When you want to know the title

Java


driver.getTitle():

C#


driver.Title;

Python


driver.title

Ruby


driver.title

When you want to get the source of the page

Java


driver.getPageSource();

C#


driver.PageSource;

Python


driver.page_source

Ruby


driver.page_source

When you want to close the window

Java


driver.close();

C#


driver.Close();

Python


driver.close()

Ruby


driver.close

When you want to close all windows

Java


driver.quit();

C#


driver.Quit();

Python


driver.quit()

Ruby


driver.quit

When you want to get an element

Java


driver.findElement(By.className("classname")); //Specify by class
driver.findElement(By.id("id")); //Specify by id
driver.findElement(By.xpath("xpath")); //Specify by xpath

C#


driver.FindElement(By.ClassName("classname")); //Specify by class
driver.FindElement(By.Id("id")); //Specify by id
driver.FindElement(By.Xpath("xpath")); //Specify by xpath

Python


driver.find_element_by_class_name("classname") #Specify by class
driver.find_element_by_id("id") #Specify by id
driver.find_element_by_xpath("xpath") #Specify by xpath

Ruby


driver.find_element(:class, "classname") #Specify by class
driver.find_element(:id, "id") #Specify by id
driver.find_element(:xpath, "xpath") #Specify by xpath

When you want to click an element

Java


driver.findElement(By.XPath("XPATH")).click();

C#


driver.FindElement(By.XPath("XPATH")).Click();

Python


driver.find_element_by_xpath("XPATH").click()

Ruby


driver.find_element(:xpath, "XPATH").click

When you want to scroll to an element

Java


WebElement element = driver.findElement(By.id("ID"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

C#


var element = driver.FindElement(By.id("ID"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();

Python


from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("ID")
actions = ActionChains(driver)
actions.move_to_element(element)
actions.perform()

Ruby


driver.find_element(:id, "ID").location_once_scrolled_into_view

When you want to select a dropdown

Java


element = driver.findElement(By.xpath("xpath"));
Select(element).selectByIndex(indexnum); //Select by index
Select(element).selectByValue("value"); //value of value
Select(element).selectByVisibleText("text"); //Display text

C#


element = driver.FindElement(By.Xpath("xpath"));
Select(element).SelectByIndex(indexnum); //Select by index
Select(element).SelectByValue("value"); //value of value
Select(element).SelectByText("text"); //Display text

Python


element = driver.find_element_by_xpath("xpath")
Select(element).select_by_index(indexnum) #Select by index
Select(element).select_by_value("value") #value of value
Select(element).select_by_visible_text("text") #Display text

Ruby


element = driver.find_element(:xpath, "xpath")
Select(element).select_by(:index, indexnum) #Select by index
Select(element).select_by(:value, "value") #value of value
Select(element).select_by(:text, "string") #Display text

When you want to enter text

Java


driver.findElement(By.id("ID")).sendKeys("string");

C#


driver.FindElement(By.id("ID")).SendKeys("string");

Python


driver.find_element_by_id("ID").send_keys("strings")

Ruby


driver.find_element(:id, "ID").send_keys("strings")

When you want to get the text

Java


driver.findElement(By.id("ID")).getText();

C#


driver.FindElement(By.id("ID")).Text;

Python


driver.find_element_by_id("ID").text

Ruby


driver.find_element(:id, "ID").text

When you want to get the attribute

Java


driver.findElement(By.id("ID")).getAttribute("value");

C#


driver.FindElement(By.id("ID")).GetAttribute("value");

Python


driver.find_element_by_id("ID").get_attribute("value")

Ruby


driver.find_element(:id, "ID").attribute("value")

When you want to handle alerts

Java


driver.switchTo().alert().accept();

C#


driver.SwitchTo().Alert().Accept();

Python


Alert(driver).accept()

Ruby


driver.switch_to.alert.accept

When you want to maximize the window size

Java


driver.manage().window().maximize();

C#


driver.Manage().Window().Maximize();

Python


driver.maximize_window()

Ruby


driver.manage.window.maximize

When you want to determine if an element is visible

Java


driver.findElement(By.xpath("xpath")).isDisplayed();

C#


driver.FindElement(By.Xpath("xpath")).Displayed(); 

Python


driver.find_element_by_xpath("xpath").is_displayed()

Ruby


driver.find_element(:xpath, "xpath").displayed?

When you want to determine if an element is valid

Java


driver.findElement(By.xpath("xpath")).isEnabled();

C#


driver.FindElement(By.Xpath("xpath")).Enabled(); 

Python


driver.find_element_by_xpath("xpath").is_enabled()

Ruby


driver.find_element(:xpath, "xpath").enabled?

When you want to determine if an element is selected

Java


driver.findElement(By.xpath("xpath")).isSelected();

C#


driver.FindElement(By.Xpath("xpath")).Selected(); 

Python


driver.find_element_by_xpath("xpath").is_selected()

Ruby


driver.find_element(:xpath, "xpath").selected?

in conclusion

I made it and saw it, but it was all similar. I would appreciate it if you could point out any mistakes. We will add it as needed if requested. Please let me know if there are other useful methods you can use: bow:

Recommended Posts

Selenium webdriver Summary of frequently used operation methods
Python + Selenium Frequently used operation method summary
[Python/Django] Summary of frequently used commands (3) <Operation of PostgreSQL>
Frequently used methods of Selenium and Beautiful Soup
[Anaconda3] Summary of frequently used commands
Summary of frequently used commands of django (beginner)
Summary of methods often used in pandas
Summary of frequently used commands in matplotlib
[Python/Django] Summary of frequently used commands (4) -Part 2- <Production operation: Amazon EC2 (Amazon Linux 2)>
List of frequently used built-in functions and methods
[Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)>
Summary of frequently used Python arrays (for myself)
[Python/Django] Summary of frequently used commands (2) <Installing packages>
Summary of frequently used commands (with petit commentary)
Summary of Pandas methods used when extracting data [Python]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Summary of scraping relations (selenium, pyautogui)
[Linux] Frequently used Linux commands (file operation)
[Linux] Frequently used Linux commands (folder operation)
[Linux] Review of frequently used basic commands 2
[Linux] Review of frequently used basic commands
Summary of built-in methods in Python list
Automatic operation of Chrome with Python + Selenium + pandas
Summary of what was used in 100 Pandas knocks (# 1 ~ # 32)
Summary of tools used in Command Line vol.8
Summary of tools used in Command Line vol.5
Summary of evaluation functions used in machine learning
[Linux command] A memorandum of frequently used commands
Summary of error handling methods when installing TensorFlow (2)
Summary of statistical data analysis methods using Python that can be used in business
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Note the frequently used options in Python + Selenium + Chrome
A collection of commands frequently used in server management
Comparison table of frequently used processes of Python and Clojure
Display a list of frequently used commands on Zsh