selenium--keyword Driver

Source: Internet
Author: User
Tags gettext xpath


Package com.dn.twohomework;

Import java.util.ArrayList;

Import Java.util.Set;
Import java.util.List;
Import Org.apache.bcel.generic.Select;
Import Org.openqa.selenium.support.ui.Select;
Import Org.apache.bcel.generic.Select;
Import Com.sun.org.apache.bcel.internal.generic.Select;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.JavascriptExecutor;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeOptions;
Import org.openqa.selenium.interactions.Actions;
Import Org.openqa.selenium.support.ui.Select;

Import Java.util.Iterator;

Import Com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;
Import Com.gargoylesoftware.htmlunit.javascript.host.Iterator;
Import Com.gargoylesoftware.htmlunit.javascript.host.Set;
Import Com.sun.media.sound.FFT;
Import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import Java.util.HashSet;;

public class KeyWord1 {
private static Webdriver driver;

Open Browse?
public void Openbrowser (String b, String XPath) {
if ("CC". Equals (b)) {

Chromedriver chrome = new Chromedriver ("Webdriver.chrome.driver", XPath);
Driver = Chrome.getdriver ();

} else if ("FF". Equals (XPath)) {
Ffdriver ff = new Ffdriver ("Webdriver.gecko.driver", XPath);
Ff.getdriver ();

} else if ("ie". Equals (b)) {

Iedriver ie = new Iedriver ("Webdriver.ie.driver", XPath);
Driver = Ie.getdriver ();
}
}

public static Webdriver Getdriver () {
return driver;
}

public static void Setdriver (Webdriver driver) {
Keyword1.driver = driver;
}

Enter URL
public void Geturl (String URL) {
try {
Driver.get (URL);


Chromeoptions options = new Chromeoptions ();
Options.addarguments ("--start-fullscreen");
Driver.manage (). window (). Maximize ();

} catch (Exception e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Open browser Lost");
E.printstacktrace ();
}
}

Wait time
public void Sleep (String time) throws NumberFormatException, Interruptedexception {

Thread.Sleep (Integer.parseint (time));
}

Input content
public void input (string xpath, string word) {
try {
Driver.findelement (By.xpath (XPath)). Clear ();
Driver.findelement (By.xpath (XPath)). SendKeys (Word);
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println ("Input string Missing");
E.printstacktrace ();
}
}

Click
public void Click (String xpath) {
try {
Driver.findelement (By.xpath (XPath)). Click ();
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println ("click Failed");
E.printstacktrace ();
}

}

Close your old browser and switch to a new window?
public void Closeoldwin () {
List<string> handle = new arraylist<string> ();
set<string> s = driver.getwindowhandles ();
Loop to get the handle in the array and save it to the list handle inside
for (iterator<string> it = S.iterator (); It.hasnext ();) {
Handle.add (It.next (). toString ());

}

Driver.close ();
Driver.switchto (). Window (Handle.get (1));

}

Close the new browser and switch to the old window?
public void Closenewwin () {
List<string> handle = new arraylist<string> ();
set<string> s = driver.getwindowhandles ();
Loop to get the handle in the array and save it to the list handle inside
for (iterator<string> it = S.iterator (); It.hasnext ();) {
Handle.add (It.next (). toString ());

}
Driver.switchto (). Window (Handle.get (1));

Driver.close ();
Driver.switchto (). Window (handle.get (0));

}

Go to the sub page?
public void Intoiframe (String XPath) {
try {
Driver.switchto (). FRAME (Driver.findelement (By.xpath (XPath)));
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println ("Failed to enter IFRAME");
E.printstacktrace ();
}

}

Close a child page?
public void Outiframe () {
Driver.switchto (). Defaultcontent ();
}

Get text messages
public void GetText (String XPath) {
System.out.println (Driver.findelement (By.xpath (XPath)). GetText ());
}

Hover
public void hover (String XPath) {
try {
Actions builder = new actions (driver);
Builder.movetoelement (Driver.findelement (By.xpath (XPath))). Build (). Perform ();
} catch (Exception e) {
TODO auto-generated Catch block

E.printstacktrace ();
System.out.println ("hover failed");
}
}
Get Links link
public string GetLink (string XPath) {
String link = "About:bliank";
try {
link = driver.findelement (By.xpath (XPath)). getattribute ("href");
} catch (Exception e) {
TODO auto-generated Catch block

E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Get link Failed");
}
return link;
}
Submit Form
public void Submit (String XPath) {
try {
Driver.findelement (By.xpath (XPath)). Submit ();
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println ("Submission failed");
E.printstacktrace ();
}
}
Select the drop-down box
public void Select (int num,string XPath) {
Select sel = new Select (Driver.findelement (By.xpath (XPath)));
Sel.selectbyindex (num);

}
Through JS operation
public void Exejs (String text) {
Javascriptexecutor js = (javascriptexecutor) driver;
Js.executescript (text);
}
Upload image
public void Uploadpiture (String name,string path) {
try {
Driver.findelement (By.name (name)). SendKeys (path);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.out.println ("Upload failed");

}
}
Get the value of an element property
public string Getsthevalueoftheelement (string xpath,string Element) {
try {
String getvalues = driver.findelement (By.xpath (XPath)). getattribute (Element);
return getvalues;
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.out.println ("Failed to get the value of the element attribute");
return null;
}
}
Click the Alert confirmation button
public void Clickalert () {
Driver.switchto (). alert (). accept ();;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.