How to Select a Specific Element in Selenium? - java

In Selenium we are able to select an entire page (by stimulating the keys Ctrl + a) using:
WebElement element = driver.findElement(By.xpath(XPath));
element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
This causes Selenium to select the entire page.
I'm trying to select only the contents of a specific web element. For example, if my XPath locates the title of a web page I want to be able to select just the title and not the entire page.
How can this be done?
Note: My goal is to combine this with: Actions(driver).moveByOffset(location.x, location.y);. First, I want to move the cursor/mouse to a element with the given coordinates (using the method moveByOffset()) and then I want to select the contents of that element (but not the entire page)..

Set the focus to the element first, assuming that it can receive focus:
element.sendKeys("");
then the second sendKeys should send the characters to that element specifically.

Related

How to handle the Wikipedia drop down list in Selenium using Java

I need help with this assignment
Go to https://www.wikipedia.org/
Perform text input in search box "apprropriate technology"
Wait for the ajax call to display the drop down of result
Return back the first suggestion
The element is hard to find in the HTML, because if you click in the browser the div with id typeahead-suggestions is hidden again. To be able to browse the HTML and search for the element I had to:
Open the developer tools
Type my text
Press F8 to pause the browser
Open the elements tab and use search to search for the text of one of the elements.
The webdriver script would be something like this:
Find the input (id: searchInput) and type a partial text
Wait for the container with items (id: typeahead-suggestions)
Get the first element with tagName A in the typeahead-suggestions container

I want to get all links from dynamic webtable column and click on each of them in Selenium

I have a dynamic webtable where it has ID column. I want to get all links from this ID column and click on it one by one. Should I use list to save all the values and using for loop iterate through it? I am new to webdriver so not sure about it.
Yes you can simply use list and store all the links.
I have a list of many hyperlinks by below text :View / Select Monitors. So I stored all the links simply in a list.
And then I can click on any link by passing the value I want.
If you want to click on all the links then you can simply use a for loop and click it.
For example:
List<WebElement> list = driver.findElements(By.linkText("View / Select Monitors"));
//Click on first hyperlink with matched text
list.get(0).click();
2nd Example:
Step1.[Getting all the links by tagname]
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
Step2. [Storing the total size for the number of links]
int s =allLinks.size();
Step 3.[I am printing all the links here and there position]
for(int i=0;iString txtlink=allLinks.get(i).getText();
System.out.println("%n"+i);//Getting the value of i and hyperlink
System.out.println(txtlink);
allLinks.get(i).click;//You can simply pass the value of i and
click on it
`Thread.sleep(2000)'
}
In this way you can get the value of "i" also and then click if you like if you need to click on any particular link and if you want to click on all then simply use .click inside the loop.`enter code here`
I hope it helps.

cannot choose a unique locator to click an element - selenium java krypton

I need to have a unique locator since i cannot use text because it both have facility, I need to click this
here is my sample code right now but it does not click
findLink(By.xpath("//*[ng-click()='promptGroupDrawerCtrl.closeDrawer()']")).click
There are two problems with the xpath:
ng-click is an attribute, not a method. Remove the round brackets.
Attribute name should start with #
findLink(By.xpath("//*[#ng-click='promptGroupDrawerCtrl.closeDrawer()']")).click();
To click on the element with text as Facility you can use the following solution:
findLink(By.xpath("//a[#class='h4 panel-heading panel-back panel-title btn ng-binding' and contains(#ng-click,'closeDrawer')]")).click
Note: As the element is an Angular element you have to induce WebDriverWait for the desired element to be clickable

Trying to choose from a SELECT tag

I have tried the code below but it's not working.
driver.get("http://www.naukri.com/");
WebElement element = driver.findElement(By.xpath("//input[#placeholder='Salary']"));
Select s1 = new Select(element);
s1.selectByVisibleText("5");
element is not a SELECT tag it just looks like one. You will need to click the INPUT element that you have in element and then click the LI that represents the desired amount, in the case listed: 5.
BTW, part of the site Terms & Conditions are that you won't scrape the site.
Naukri.com uses technological means to exclude Robots etc from crawling the website and scraping content. The user undertakes not to circumvent these methods.

Any way to pass data from a HTML to a Java application?

I have a JavaScript code embedded in a Java method to retrieve properties of any element clicked upon in a web page.
That is, I navigate to a specific URL by creating an instance of Selenium WebDriver from my Java application and click on an object in the page that gets loaded. After the single click on the object, a HTML page opens listing the name, type, id, class name etc. associated to that object as a radio button group. I select one option from the list and click on OK. What I wanted to know is, can I push the selected data from the HTML back onto my Java application on the click event on the HTML?
Any help appreciated! Thanks! :)
WebElement interface of selenium Webdriver gives a method
getAttribute which provides value of a given attribute of the desired html element.
I hope this method is what you are looking for.
USAGE:
String field_value;
field_value = driver.findElement(By.xpath("xpath Locator")).getAttribute("attribute name")
The information which you want to get should be in any of the attribute associated to that element.
Similarly if you want all attributes associated with the element in one go you can use something like this :
((JavascriptExecutor)driver).executeScript("return arguments[0].attributes);", element);
The above will give you a comma separated list of all attributes on which you can apply your coding logic to separate them and get the corresponding values using loops.

Categories