Edit :
The element was inside an iframe, this is how it finally worked:
WebDriverWait wait = new WebDriverWait(_driver, 60);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
.xpath("//*[#class='IFrameID']")));
WebElement element_t = _driver.findElement(
By.xpath("//*[#myattribute='mytest']"));
Edit :
My problem seems to be the structure of the page. i tried different things, and i only was able to get the body by id, ever other element i tried to get by id or any other attribute couldnt be found...
I am trying to get an element by using the By.xpath method, the xpath itself works just fine when used in firebug/firepath, but when used in the java application i am getting an Exception:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#myattribute='mytest']"}
The attribute i am trying to access is not a standard html one, but generated from a framework so the field looks like this :
<input id="F_19" class="FIELDInputEdit" type="text" style=" width:100%;" maxlength="40" myattribute="mytest" name="CC">
The javacode itself looks like this :
WebElement element_t = _driver.findElement(
By.xpath(".//*[#myattribute='mytest']"));
Since the only known attribute is this one, i have no ohter way to access the input field.
I am using Firefox 17.0.11
good practice (imho) before using xpath in webdriver test it using selenium ide (http://docs.seleniumhq.org/download/)
Error might be because of «.» before «//». Try to remove it.
Read this: http://www.w3schools.com/xpath/xpath_syntax.asp
.//*[#myattribute='mytest']
«.» = Selects the current node
«//» = Selects nodes in the document from the current node that match the selection no matter where they are
«*» = Matches any element node
«[#myattribute='mytest']» ( = [#myattribute = \"mytest\"]) = Node, that contains attribute "myattribute", which value is "mytest"
Now, _driver.findElement(By.xpath("//*[#myattribute='mytest']")) = search whole page for first node with attribute «myattribute» with value «mytest»
_driver.findElement(By.xpath("//input[#myattribute='mytest']"))
= search whole page for first input with «myattribute» = «mytest»
_driver.findElement(By.xpath("//input[#myattribute='mytest']")).then(By.path("./*[#comeAtr]")) = in input with «myattribute» = «mytest» find any node with atribute = «
Have you tried to use CSS selectors instead?
By.cssSelector("input[myattribute=\"mytest\"]")
The element was inside an iframe, this is how it finally worked:
WebDriverWait wait = new WebDriverWait(_driver, 60);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
.xpath("//*[#class='IFrameID']")));
WebElement element_t = _driver.findElement(
By.xpath("//*[#myattribute='mytest']"));
Related
I am using Selenium with Java and I have encountered issue that I am unable to find div by class name, even though it is unique:
<div class="123123-randomclassname"></div>
I am able to find any other element, e.g. input, button, etc. I have issues with div tag only.
I have tried getting this web element using either #FindBy() annotation and findElement() method:
driver.findElement(By.className("123123-randomclassname"))
driver.findElement(By.cssSelector("div[class='123123-randomclassname'"))
#FindBy(className = "123123-randomclassname")
#FindBy(css = "div[class='123123-randomclassname'")
Any of these solutions did not work and I couldn't find element.
Try with following css selector
driver.findElement(By.cssSelector("div[class^='123123-']"))
//# and ]closing bracket was missing in syntax
WebElement DivTag = driver.findElement(By.xpath("div[#class='123123-randomclassname']"));
//OR
#FindBy(xpath = "div[#class='123123-randomclassname']") WebElement DivTag2;
I want to get Selenium with Chromedriver to recognize and import a line of html text into a Webelement variable.
Given this HTML:
<li id="password_rules">
<strong>Password Rules</strong>
<p>The database password rules conform with...:</p>
<ul>
<li>The password can not be the same as the user id.</li>
<li>Password length must be between 8 and 25 characters.</li>
</ul>
</li>
I want to grab the text in the last list element ("Password length must be between 8 and 25 characters.").
This is the java code I'm attempting to use:
WebElement passwordCriteria = driver.findElement(By.xpath("//*[#id = 'password_rules']//*[contains(text(), 'Password length must be between ')]"));
String lineText = passwordCriteria.getText();
When that Java executes, it returns an error saying the element cannot be found:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable
to locate an element with the xpath expression //[contains((text(),
'Password length must be between ')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string
'//[contains((text(), 'Password length must be between ')]' is not a
valid XPath expression.
Any insight is much appreciated.
If you are grabbing a WebElement by it's id, then you don't need your extra specific xPath. id is supposed to be unique by convention. If you have other html elements with the same id, consider changing them to a class.
You should be able to grab the element like this:
WebElement passwordCriteria = driver.findElement(By.id("password_rules"));
If you're committed to finding the element by the id containing some text then the way you should do it is as follows:
WebElement passwordCriteria = driver.findElement(By.xpath("//*[contains((text(),'Password length must be between')]"));
Also, sometimes Selenium will complain if elements are not visible on the page when you try and reference them. Sometimes you need to wait, other times you need to perform some other action to make the text visible before referencing it, like hovering the mouse over a dropdown, etc.
For example
Suppose the html you pasted here is an error message. This error message does not start out as being 'visible', but instead it is shown after the user types their password in incorrectly. In such an instance, Selenium won't let you reference the text from an element inside this div, since it's not currently view-able. Instead, what you would have to do is use Selenium to input the incorrect password in the fields, wait for the error message to be displayed, and then finally reference the WebElement, only after it is able to be seen.
EDIT:
I misread OP's intention. The element that OP is trying to reference is NOT the element with the id, but rather a child of that element. Instead of rewriting my answer, I will point out that #Grasshopper answer has both css and xPath solutions.
You can try these locators if the concerned li is always the last child.
Css - "li[id='password_rules'] > ul > li:last-child"
xpath - "//li[#id='password_rules']/ul/li[last()]"
As per your question as the desired text is within Password Rules you have to induce WebDriverWait with ExpectedConditions as textToBePresentInElementLocated and then retrieve the text as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//li[#id='password_rules']//ul//li"), "Password length"));
String lineText = driver.findElement(By.xpath("//li[#id='password_rules']//ul//li[contains(.,'Password length')]")).getAttribute("innerHTML");
Thank you for the help everyone. I finally got it using the following:
new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//li[#id = 'password_rules']"), "Password length must be between "));
WebElement passwordCriteria = driver.findElement(By.xpath("//li[#id = 'password_rules']/ul/li[2]);
String lineText = passwordCriteria.getText();
Your original example had // which should only be used at the beginning of an xpath.
So I have this HTML element:
<h2 class="post-title">
This a link to Google!
</h2>
I am using driver.find_elements_by_class_name('post-title') to find this piece of HTML.
But how can I extract only the value of the "href" tag?
I've tried:
driver.get_attribute('href')
returns 'none'
Thanks
You have 2 problems:
you're trying to find h2 elements instead of a
you're trying to get attribute value from WebDriver instance
Try below code to get required output:
driver.find_element_by_css_selector('h2.post-title>a').get_attribute('href')
href belongs to <a> tag; so first you have to reach till that element as following:
elem = driver.find_element_by_xpath('//h2[#class="post-title"]/a')
attribute_value = elem.get_attribute('href')
Indeed the sibling whose tag is h2 has no href attribute and this is the one you target via searching elements by_class_name('post-title') . It is the sibling <a></a> which does.
What about searching by xpath ? If 'post-title' is a unique class identifier, you can search for your element as follows
xpth = "//*[#class='post-title']/a"
a_element = driver.find_element_by_xpath(xpth)
and finally
href = a_element.get_attribute('href')
Something you could do from what you (almost) have
h2_element = driver.find_element_by_class_name('post-title')
a_element = h2_element.find_element_by_tag_name("a")
href = a_element.get_attribute('href')
I am trying to get value of HREF attribute but always it says incorrect Xpath.
Html Code :
I am trying code :
WebElement Link = driver.findElement(By.xpath("//table[contains(#class,'display')]/thead/tbody/tr/td/a"));
System.out.println(Link.getAttribute("href"));
I tried many xpath but none of them worked.
This should work:
WebElement Link = driver.findElement(By.xpath("//table[contains(#class,'display')]/tbody/tr/td/a"));
Explanation: tbody is not nested in thead.
Note that a path expression like this can return a set of several nodes, in document order. The findElement method only returns the first result node. So: if the a element you are looking for is no longer the first one in this table, the path expression breaks.
If the the href is unique, something like this would be less error-prone:
WebElement Link = driver.findElement(By.xpath("//table[contains(#class,'display')]//a[#href='/admin/client/product_overrides/edit/242625']"));
I am creating an automatic test for some webs and I'm using WebDriver, TestNG and code that is written in Java. On the page is shown register of categories, in parentheses is number of auctions and i need to get this number as variable, but i don't know, how.
I use the following code:
WebElement number1_1_vse = (driver.findElement(By.linkText("Vše")));
String text_vse1_1 = number1_1_vse.getText();
but output is only: "Vše" without (number of auctions)
link to the website
screenshot -> image with the number
can anyone advise me please?
With linktext you are finding the nested a which text is Vše only. You need to find the li containing the text Vše (949)
Use the following css selector to identify the element
By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
WebElement parent = number1_1_vse.findElement(By.xpath(".."));
// which will give you access to <li>
List<WebElement> childs = parent.findElements(By.xpath(".//*"));
// childs.get(1) has the result.
Please try to get the xpath value and then try to search it with below mentioned syntax :
findelementbyxpath("//*[#id="post-form"]/h2")
To get xpath value of and element : right click --> inspect element --> right click copy xpath