I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.
Related
Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));
I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);
i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();
I have a div element which looks like below.
I identify this element with the following xpath.
//*[contains(#class,'ce-component-title')]
Selenium identifies this element and loads the WebElement object. But when I go to get its text, I'm just getting a "." as shown below instead of getting "Purchase to pay process". What am I doing wrong here? I checked the chrome console and there's no other element matching this xpath.
Any help would be much appreciated.
As per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:
cssSelector:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[ng-if*='getTitle']>div.ce-component-title"))).getAttribute("innerHTML");
xpath:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#ng-if,'getTitle')]/div[#class='ce-component-title flex text-overflow ng-binding']"))).getAttribute("innerHTML");
I think your xpath (//*[contains(#class,'ce-component-title')]) returns more than one element, and the element you need is not the first one.
Try using console of your browser, to verify how many elements this xpath returns.
I can help you if you give me url to this page.
Html:
<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong>
my xpath:
#FindBy(how = How.XPATH, using = "//strong[#ng-bind='summary.PublishedIssuedRecent']")
public WebElement countRecentlyPublishedApplication;
String pubCount = countRecentlyPublishedApplication.getText();
my Expectation
i want to extract the value of 5
but i'm getting empty value in the pubCount String, Please suggest if any mistake in my xpath
From attribute ng-bind I'm guessing you're trying to scrap angularJS application and you're using selenium web driver.
Problem with angular is, it is rendered by javascript after the page loads. So on beginning element you want to scrap might not be there. Solution might be to wait a second until angulars finishes rendering and then try to find element.
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
WebElement pubCount = driver.findElement(By.xpath( "//strong[#ng-bind='summary.PublishedIssuedRecent']")).getText();
The XPath expression in itself is correct, it selects your node as expected. A simple test with xmllint gives:
> xmllint --xpath "//strong[#ng-bind='summary.PublishedIssuedRecent']" ~/test.html
<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong>
So the error is outside your XPath expression.
By the way, if you just need the text of the node, you can use
> xmllint --xpath "//strong[#ng-bind='summary.PublishedIssuedRecent']/text()" ~/test.html
5