public static void main(String[] args)
{
WebDriver wd = new FirefoxDriver();
wd.manage().window().maximize();
wd.get("http://www.arthritisspecialityclinic.com");
WebElement link=wd.findElement(By.linkText("CONTACTS"));
link.click();
WebElement Name = wd.findElement(By.xpath(".//*[#id='contact-form']/fieldset/label[1]/span[3]"));
Name.sendKeys("sakthivel");
}
I've executed the above code for Enter text in NAME text box under contact Form in website..But the text is not typed in the specific field only shown Blank...No error also shown in web driver...Any one can help me to Fix this....
Your xPath is wrong. You should select the input tag instead of the span tag. Try this:
WebElement name = wd.findElement(By.xpath("//form[#id='contact-form']/fieldset/label[1]/input"));
name.sendKeys("sakthivel");
Suggestion: This would be a cleaner method to select the WebElement.
WebElement name = wd.findElement(By.xpath("//input[#name='name']"));
name.sendKeys("sakthivel");
Related
I want to get a text from a page which will be opened after I login, but I don't have an idea how to do so, currently the URL is https://www.tdscpc.gov.in/app/login.xhtml after login, it opens a page https://www.tdscpc.gov.in/app/ so I want to get a Text from the second page,
thanks
static WebDriver driver = new ChromeDriver();
public static void main(String[] args) throws InterruptedException {
String exePath = "C:\\Users\\Dell\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
driver = new ChromeDriver();
driver.get("https://www.tdscpc.gov.in/app/login.xhtml");
driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[3]/table/tbody/tr/td[1]/input"))
.click();
driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[4]/p[2]/input"))
.sendKeys("*******");
driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[6]/p[2]/input"))
.sendKeys("*******");
driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[8]/p[4]/input[3]")).
sendKeys("*******");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
while (driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[12]/p[2]/input")).o) {
//This is the value which is on the new page, i want to get this value from the page, the page opens automatically once login is done, i just want to get this element froom a page which opens after login (Like the Dashboard)
String name = driver.findElement(By.xpath("/html/body/div[1]/div[3]/text()")).getText();
String nameInReplace = name.replace("Welcome ", "");
System.out.println(nameInReplace);
}
}
Edit2-
I Think that the Driver is getting the Element from the first URL, which is invalid as the element is not present there, how do i change it so it detects it from the new page and not the first page?
maybe u can use
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("...")));
it waits until the element you want to reach appears
I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown
I have been unable to sendKeys text into the username and password field on ebay.
Here is the code:
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.ebay.co.uk");
WebElement myEbay = driver.findElement(By.linkText("My eBay"));
myEbay.click();
WebElement signInForm = driver.findElement(By.id("SignInForm"));
if (signInForm.isDisplayed())
System.out.println("Sign in form is displayed");
WebElement username;
username = driver.findElement(By.cssSelector("input[placeholder=\"Email or username\"]"));
It manages to find the My Ebay link, and verifies that the sign in form exists but the the username and password fields id's change after every refresh of the page.
The username cssSelector seems to be the problem??
EDIT: I have been successful using XPath but this excercise was to make the cssSelector work as there is no reason in theory why it shouldn't!
I have replaced the cssSelector locator with xpath and it is working fine with absolute xpath. Here is the modified code:
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vikas\\workspaceNeon\\Eclipse Soft\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.ebay.co.uk");
WebElement myEbay = driver.findElement(By.linkText("My eBay"));
myEbay.click();
WebElement signInForm = driver.findElement(By.id("SignInForm"));
if (signInForm.isDisplayed())
System.out.println("Sign in form is displayed");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement username = driver.findElement(By.xpath("html/body/div[4]/div/div/div/div[5]/div/div[1]/div/div[1]/div[1]/div[2]/div/span/form/div[1]/div[5]/div/div[4]/span[2]/input"));
username.sendKeys("Vikas");
Select all inputs as a list and search for the right one:
// fld seems to be the class of the input field but looking at all input elements should work too
List<WebElement> inputs = driver.findElements(By.className("fld"));
for (WebElement input: inputs) {
if (inputs.getAttribute("placeholder") == "Email or username") {
// ...
}
}
if the username and password webelements changes frequently find div section in the page using xpath and get the exact xpath of username weblement.
Hope this help..
I am identifying a clickable WebElement by CssSelector. An example code I am using is:
String selectorString = "a.some-text.item";
WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();
The problem is, that after the item has been clicked its CssSelector changes to a.some-text.item.clicked. I can still securely identify it by using above selectorString variable. However, I only want to click it if it has not been clicked before.
How can I determine the items status, i.e. whether its cssSelector name is a.some-text.item or a.some-text.item.clicked?
I only want to click it if it has not been clicked before.
Try using :not(selector) function of cssSelector to determine only unclicked element :-
String selectorString = "a.some-text.item:not(.clicked)";
WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();
Edited :- If you want to just determine whether element has contains clicked class or not after clicking then try as below :-
String selectorString = "a.some-text.item";
driver.findElement(By.cssSelector(selectorString)).click();
//Now verify element clicked or not
WebElement we = driver.findElement(By.cssSelector(selectorString));
String msg = (we.getAttribute("class").contains("clicked")) ? "element clicked" : "element not clicked";
System.out.println(msg);
I need to automate login on website and then choosing links randomly from the list of links. After each test run, new link should be selected randomly from list.
I have already automate login:
public class Test1{
public static void main(String[] args)
String path = System.getProperty("user.dir");
System.out.println(path);
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get ("http://test.com");
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("brr#gmai.com");
WebElement password = driver.findElement(By.id("pass"));
password.sendKeys("1111");
Now, I have list of links and need to select them randomly. How could I do it ?
To get list of links use driver.findElements(By.tagName("a")) and then to select one at random use a random number generator and select the WebElement at the array index.
http://docs.seleniumhq.org/docs/03_webdriver.jsp
Try this
Random r = new java.util.Random();
List<WebElement> links = driver.findElements(By.tagName("a"));
WebElement randomElement = links.get(r.nextInt(links.size()));