Best Selenium code snippet using org.openqa.selenium.By.xpath
Source:Facebook2.java
...113 114 }115 public static void invokeButton()throws Exception116 {117 driver.findElement(By.xpath(OR.LogoutInvokeButton)).click();118 }119 120 public static void password()throws Exception121 {122 Facebook2.GetInputData();123 driver.findElement(By.xpath(OR.Password)).sendKeys(Facebook2.Pwd);124 }125 126 public static void emailName()throws Exception127 {128 Facebook2.GetInputData();129 driver.findElement(By.xpath(OR.email)).sendKeys(Facebook2.Uname);130 }131 132 public static void clickButon()throws Exception133 {134 135 Facebook2.wait5seconds();136 Facebook2.wait5seconds(); 137 driver.findElement(By.xpath(OR.LoginSubmit)).click();138 Facebook2.wait5seconds();139 }140 public static void logoutBrowser()throws Exception141 {142 // driver.findElement(By.xpath(" //div[text()='Account Settings'] ")).click();143 Facebook2.wait5seconds(); 144 145 driver.findElement(By.xpath(OR.LogoutButton)).click();146 147 }148 public static void closeBrowser()throws Exception149 {150 driver.quit();151 /*152 driver.findElement(By.xpath(OR.email)).sendKeys("sudhakarth2008@gmail.com");153 driver.findElement(By.xpath(OR.Password)).sendKeys("sudhakarth2008@gmail.com");154 driver.findElement(By.xpath(OR.LoginSubmit)).click();155 driver.findElement(By.xpath(OR.LogoutInvokeButton)).click();156 driver.findElement(By.xpath(OR.LogoutButton)).click();157 158 */159 160 }161 public static void wait5seconds()throws Exception162 {163 try {164 Thread.sleep(3000);165 } catch (InterruptedException e) {166 e.printStackTrace();167 }168 169 170 }...
Source:AccountCreationPage.java
...67 public static void EnterAcountDetails()throws Exception68 {69 driver=OR.driver;70 Signinpage.wait5seconds();71 driver.findElement(By.xpath(OR.ACP_MR_button)).click();72 Signinpage.wait5seconds();73 driver.findElement(By.xpath(OR.ACP_firstname_text)).sendKeys("thatichettu");74 75 driver.findElement(By.xpath(OR.ACP_lastname_text)).sendKeys("sudhakar");76 //driver.findElement(By.xpath(OR.ACP_email_textbox)).sendKeys("sudhakarth2008@gmail.com");77 Signinpage.wait5seconds();78 driver.findElement(By.xpath(OR.ACP_password_text)).sendKeys("babli1234");79 Signinpage.wait5seconds();80 Signinpage.wait5seconds();81 Select listbox =new Select(driver.findElement(By.xpath(OR.ACP_DOB_DD_Listbox)));82 //listbox.selectByVisibleText("15");83 listbox.selectByValue("15");84 85 86 Signinpage.wait5seconds();87 Select listbox1 =new Select(driver.findElement(By.xpath(OR.ACP_DOB_MMM_Listbox)));88 //listbox1.selectByVisibleText("June");89 listbox1.selectByValue("6");90 Select listbox2 =new Select(driver.findElement(By.xpath(OR.ACP_DOB_year_Listbox)));91 //listbox2.selectByVisibleText("1981");92 listbox2.selectByValue("1981");93 94 95 driver.findElement(By.xpath(OR.signup_click)).click();96 Signinpage.wait5seconds();97 driver.findElement(By.xpath(OR.receivesspecial)).click();98 Signinpage.wait5seconds();99 driver.findElement(By.xpath(OR.firstName_text)).sendKeys("sudhakar");100 driver.findElement(By.xpath(OR.lastName_text)).sendKeys("thatichettu");101 102 driver.findElement(By.xpath(OR.company_text)).sendKeys("tata"); 103 driver.findElement(By.xpath(OR.address_text)).sendKeys("miyapur");104 driver.findElement(By.xpath(OR.adress2_text)).sendKeys("jntu");105 driver.findElement(By.xpath(OR.city_text)).sendKeys("hyd");106 Signinpage.wait5seconds();107 // driver.findElement(By.xpath(OR.state_textadress2_text)).sendKeys("5");108 109 Select listbox3 =new Select(driver.findElement(By.xpath(OR.state_textadress2_text)));110 //listbox.selectByVisibleText("15");111 listbox3.selectByValue("5");112 driver.findElement(By.xpath(OR.postcode)).sendKeys("123");113 114 Select listbox4 =new Select(driver.findElement(By.xpath(OR.unitedstate)));115 //listbox.selectByVisibleText("15");116 listbox4.selectByValue("21");117 driver.findElement(By.xpath(OR.additionalinformation)).sendKeys("subha");118 119 driver.findElement(By.xpath(OR.home_phone_text)).sendKeys("23456");120 driver.findElement(By.xpath(OR.mobilephone_text)).sendKeys("9441127463");121 driver.findElement(By.xpath(OR.homeaddress_text)).sendKeys("narasaraopet");122 123 driver.findElement(By.xpath(OR.register_click)).click();124 125 driver.quit();126 127 128 129 130 131 132 133 134 135 136 137 ...
Source:MercuryFlightFinderPage.java
...101public static void MerucryFlightFinderBusinessEconomyFirst() throws Exception102{103 driver=OR.driver;104 105 driver.findElement(By.xpath(OR.MFFPage_OneWayRadiobutton)).click();106 Select listbox =new Select(driver.107 findElement(By.xpath(OR.MFFPage_PassengersCountListbox)));108 listbox.selectByVisibleText("2");109 110 Select listbox1 =new Select(driver.111 findElement(By.xpath(OR.MFFPage_DepartingFromListbox)));112 listbox1.selectByVisibleText("London");113 114 Select listbox2 =new Select(driver.115 findElement(By.xpath(OR.MFFPage_DepartingStartMonthListbox)));116 listbox2.selectByVisibleText("February");117 118 Select listbox3 =new Select(driver.119 findElement(By.xpath(OR.MFFPage_DepartingStartDateListbox)));120 listbox3.selectByVisibleText("10");121 122 Select listbox4 =new Select(driver.123 findElement(By.xpath(OR.MFFPage_ArrivingIn)));124 listbox4.selectByVisibleText("Paris"); 125 126 Select listbox5 =new Select(driver.127 findElement(By.xpath(OR.MFFPage_ReturningEndMonthListbox)));128 listbox5.selectByVisibleText("March");129 130 Select listbox6 =new Select(driver.131 findElement(By.xpath(OR.MFFPage_ReturningEndDateListbox)));132 listbox6.selectByVisibleText("14");133 134 135 if ( MercuryFlightFinderPage.Reservation.equals("Economy"))136 {137 driver.findElement(By.xpath(OR.MFFPage_EconomyclassRadioButton)).click();138 }139 else if ( MercuryFlightFinderPage.Reservation.equals("Business "))140 {141 driver.findElement(By.xpath(OR.MFFPage_BusinessclassRadioButton)).click();142 }143 else144 {145 driver.findElement(By.xpath(OR.MFFPage_FirstClassRadioButton)).click();146 //otherwise first class147 }148 149 150 151 Select listbox7 =new Select(driver.152 findElement(By.xpath(OR.MFFPage_AirLineListbox)));153 listbox7.selectByVisibleText("Unified Airlines");154 155 156 driver.findElement(By.xpath(OR.MFFPage_ContinueButton)).click();157 158}159 160}...
Source:passengers.java
...66 67 68 driver=OR.driver ;69 70 driver.findElement(By.xpath(OR.firstname_text)).sendKeys("sudhakar");71 driver.findElement(By.xpath(OR.lastname_text)).sendKeys("thatichettu");72 Select listbox =new Select(driver.findElement(By.xpath(OR.meal_click)));73 listbox.selectByVisibleText("Hindu");74 75 Select listbox5 =new Select(driver.findElement(By.xpath(OR.cardtype_text)));76 listbox5.selectByVisibleText("Visa");77 78 // Select listbox6 =new Select(driver.findElement(By.xpath(OR.creditnumber_text)));79 //listbox6.selectByVisibleText("1234 5678 9123 4567");80 81 driver.findElement(By.xpath(OR.creditnumber_text)).sendKeys("1234 5678 1234 3456");82 83 84 Select listbox7 =new Select(driver.findElement(By.xpath(OR.creditcardmonth_text)));85 listbox7.selectByVisibleText("02");86 87 88 Select listbox8 =new Select(driver.findElement(By.xpath(OR.creditcardyear_text)));89 listbox8.selectByVisibleText("2010");90 91 driver.findElement(By.xpath(OR.creditcardfirstname_sendkeys)).sendKeys("sudhakar");92 driver.findElement(By.xpath(OR.creditcardmiddlename_sendkeys)).sendKeys("sudha");93 driver.findElement(By.xpath(OR.creditcardlastname_sendkeys)).sendKeys("thatichettu");94 95 96 97 driver.findElement(By.xpath(OR.billingaddress_sendkeys)).sendKeys("hyderabad");98 driver.findElement(By.xpath(OR.billingaddress2_sendkeys)).sendKeys("miyapur");99 driver.findElement(By.xpath(OR.city_sendkeys)).sendKeys("sanathnagar");100 driver.findElement(By.xpath(OR.state_sendkeys)).sendKeys("telangna");101 driver.findElement(By.xpath(OR.postalcode_sendkeys)).sendKeys("1232456");102 103 104 Select listbox9=new Select(driver.findElement(By.xpath(OR.countryname_sendkeys)));105 listbox9.selectByVisibleText("TURKEY");106 107 108 /* driver.findElement(By.xpath(OR.billingaddress_sendkeys)).sendKeys("eluru");109 driver.findElement(By.xpath(OR.city1_sendkeys)).sendKeys("amaravathi");110 driver.findElement(By.xpath(OR.state1_sendkeys)).sendKeys("ap");111 driver.findElement(By.xpath(OR.postalcode1_sendkeys)).sendKeys("500006");112 driver.findElement(By.xpath(OR.country_sendkeys)).sendKeys("UNITEDSTATES");*/113 driver.findElement(By.xpath(OR.securepurchage_click)).click();114 115 116 117 118 119 120 121 122 123 124 }125}...
Source:Bubbly3.java
...65 66 driver.manage().window().maximize() ; 67 driver.get(baseUrl);68 69 driver.findElement(By.xpath(OR3.MLPage_UserNameTextbox)).sendKeys("mercury");70 driver.findElement(By.xpath(OR3.MLPage_PasswordTextbox)).sendKeys("mercury");71 driver.findElement(By.xpath(OR3.MLPage_SignInButton)).click();72 // driver.quit();73 try {74 Thread.sleep(3000);75 } catch (InterruptedException e) {76 e.printStackTrace();77 }78 79 driver.findElement(By.xpath(OR3.OneWayJourney)).click();80 81 try {82 Thread.sleep(3000);83 } catch (InterruptedException e) {84 e.printStackTrace();85 }86 87 88 89 driver.findElement(By.xpath(OR3.busuinessclass)).click();90 91 try {92 Thread.sleep(3000);93 } catch (InterruptedException e) {94 e.printStackTrace();95 }96 97 //driver.findElement(By.xpath(OR3.MLPage_UserNameTextbox)).sendKeys("mercury"); 98 Select listbox =new Select(driver.findElement(By.xpath(OR3.airline)));99 listbox.selectByVisibleText("Unified Airlines"); 100 try {101 Thread.sleep(3000);102 } catch (InterruptedException e) {103 e.printStackTrace();104 }105 Select listbox1 =new Select(driver.findElement(By.xpath(OR3.arrivingin)));106 listbox1.selectByVisibleText("London");107 try {108 Thread.sleep(3000);109 } catch (InterruptedException e) {110 e.printStackTrace();111 }112 Select listbox2 =new Select(driver.findElement(By.xpath(OR3.departingfrom)));113 listbox2.selectByVisibleText("Paris");114 try {115 Thread.sleep(3000);116 } catch (InterruptedException e) {117 e.printStackTrace();118 }119 driver.findElement(By.xpath(OR3.departingfrom)).click();120 121 try {122 Thread.sleep(9000);123 } catch (InterruptedException e) {124 e.printStackTrace();125 }126 127 128 129 // driver.quit();130 131 }132 133}...
Source:Facebook6.java
...56 public WebDriver driver ; 57 @Test58 public void FBLogiexecution() throws Exception{59 60 driver.findElement(By.xpath(" //input[@type='email' and @name='email'] ")).sendKeys("sudhakarth2009@gmail.com");61 driver.findElement(By.xpath(" //input[@type='password' and @name='pass'] ")).sendKeys("babli1234");62 driver.findElement(By.xpath(" //input[@type='submit'and @id='u_0_2'] ")).click();63 64 65 try {66 Thread.sleep(6000);67 } catch (InterruptedException e) {68 e.printStackTrace();69 }70 71 driver.findElement(By.xpath(" //div[text()='Account Settings'] ")).click();72 73 try {74 Thread.sleep(3000);75 } catch (InterruptedException e) {76 e.printStackTrace();77 }78 79 driver.findElement(By.xpath(" //text()[.='Log Out']/ancestor::span[1] ")).click();80 81 82 }83 84 @BeforeTest85 public void setBaseURL()86 {87 88 89 ChromeOptions options = new ChromeOptions();90 options.addArguments("--disable-notifications");91 System.setProperty("webdriver.chrome.driver",driverPath);92 driver =new ChromeDriver(options);93 driver.get(baseUrl);94 driver.manage().window().maximize() ;95 96 }97 98 99 //driver.findElement(By.xpath(" ")).sendKeys(" ");100 //driver.findElement(By.xpath(" ")).click();101 102 @AfterTest103 public void afterTest() {104 105 106 driver.quit();107 108 }109 110}...
Source:Facebook.java
...56 public WebDriver driver ; 57 @Test58 public void FBLogiexecution() throws Exception{59 60 driver.findElement(By.xpath(" //input[@type='email' and @name='email'] ")).sendKeys("sudhakarth2009@gmail.com");61 driver.findElement(By.xpath(" //input[@type='password' and @name='pass'] ")).sendKeys("babli1234");62 driver.findElement(By.xpath(" //input[@type='submit'and @id='u_0_2'] ")).click();63 64 65 try {66 Thread.sleep(6000);67 } catch (InterruptedException e) {68 e.printStackTrace();69 }70 71 driver.findElement(By.xpath(" //div[text()='Account Settings'] ")).click();72 73 try {74 Thread.sleep(3000);75 } catch (InterruptedException e) {76 e.printStackTrace();77 }78 79 driver.findElement(By.xpath(" //text()[.='Log Out']/ancestor::span[1] ")).click();80 81 82 }83 84 @BeforeTest85 public void setBaseURL()86 {87 88 89 ChromeOptions options = new ChromeOptions();90 options.addArguments("--disable-notifications");91 System.setProperty("webdriver.chrome.driver",driverPath);92 driver =new ChromeDriver(options);93 driver.get(baseUrl);94 driver.manage().window().maximize() ;95 96 }97 98 99 //driver.findElement(By.xpath(" ")).sendKeys(" ");100 //driver.findElement(By.xpath(" ")).click();101 102 @AfterTest103 public void afterTest() {104 105 106 driver.quit();107 108 }109 110}...
Source:flightpage1.java
...60 }61 public static void flightpageBrowser()throws Exception62 {63 driver=OR.driver;64 driver.findElement(By.xpath(OR.oneway_click)).click();65 mercury.wait5seconds();66// driver.findElement(By.xpath(OR.passengers_click)).click();67 Select listbox1 =new Select(driver.findElement(By.xpath(OR.passengers_click)));68 listbox1.selectByVisibleText("1");69 Select listbox2=new Select(driver.findElement(By.xpath(OR.departure_click)));70 listbox2.selectByVisibleText("London");71 72 Select listbox3=new Select(driver.findElement(By.xpath(OR.month_click)));73 listbox3.selectByVisibleText("March");74 75 76 Select listbox4=new Select(driver.findElement(By.xpath(OR.date_click)));77 listbox4.selectByVisibleText("20");78 79 Select listbox5=new Select(driver.findElement(By.xpath(OR.ariving_click)));80 listbox5.selectByVisibleText("Paris");81 82 driver.findElement(By.xpath(OR.service_click)).click();83 84 driver.findElement(By.xpath(OR.continue1_click)).click();85 86 87 88 89 90 91 }92 93 94}...
xpath
Using AI Code Generation
1package com.selenium4beginners.java.webdriver;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.firefox.FirefoxDriver;6public class ByXpath {7public static void main(String[] args) {8WebDriver driver = new FirefoxDriver();9System.out.println(element.getText());10driver.quit();11}12}13public static By xpath(String xpathExpression)14public static By xpath(String xpathExpression)15public static By xpath(String xpathExpression)
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!