I have upgraded my selenium framework to selenium 3.4 version and I am getting the below errors:
If the Class Name has a space then FindElement fails to return list of ReadOnlyCollection<IWebElement>
What steps will reproduce the problem?
1.Not working:
FindElements(By.ClassName("CLASS NAME"));
2. Working:
FindElements(By.ClassName("CLASSNAME"));
Now most of xpath contains class name with spaces and my scripts`are not working.
To make all xpaths in my script without spaces is a huge task. It will take more time to maintain this.
Can anyone suggest me or do you have any work-around to make a minimal change and script will be fine?
Also will the selenium team fix this issue?
The thing is that there is no spaces allowed in a classname
If you are seeing classname like :
<input class="class name"/>
then it represents 2 classes first is class and second is name
Now come to selenium :
By.className() selector doesn't allows you to use compound classes. like By.className("class name")
If you want to manipulate compound class the you have to use xpath in this way
driver.findElement(By.xpath("//span[#class='class name']")
OR CSS Selector in this way
driver.findElement(By.cssSelector(".class.name")
Related
I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.
I am not able to locate the chrome downloaded filename using locator. I need to get the filename to proceed further on automation to verify the content.
Element HTML:
<a is="action-link" id="file-link" focus-row-control="" focus-type="fileLink" tabindex="0" role="link" href="https://energyfuture.cgi.com/pasdemo/operations/QueryDisplay!displayQuery.action?QUERY_TIMESTAMP=1614571607262" class="">Facility Balance_1614571607828.xlsx</a>
Here the numbers of filename are dynamic, but the prefix Facility Balance will be constant.
code I have tried:
driver.navigate().to("chrome://downloads/");
String Text = driver.findElement(By.xpath("//*[#id='file-link']")).getText();
OR
String AttributeData = driver.findElement(By.xpath("//*[contains(text(),'Facility Balance')]")).getAttribute("linkText");
For starters
.getAttribute("linkText");
will not work because your target node does not contain an attribute called linkText.
The second point,
By.xpath("//*[#id='file-link']")
might not work if you have multiple elements with the same ID on the page (this include popups). Consider refining your xpath to be more explicit. For example, By.xpath("//a[#id='file-link' and ...]") where the ... are additional conditions that will refine your search to find only one.
Out of curiosity, did you try your Xpath directly in the browser developer's tools? Before you attempt to code things like this, you should always attempt to evaluate your XPath expressions to make sure that 1) they are accurate and 2) they are not ambiguous. Unfortunately, given the amount of information posted, this is the extent of the help I can give you.
I am trying to capture a drop-menu field using test automation in SELENIUM with chrome driver.
Why does the element "id" changes in some web pages when inspecting elements time to time, with Chrome browser? How to keep the "id"s static, without changing?
Steps I followed:
When I inspect elements in the web page, the particular drop-menu shows its "id" as: id="combo-1782-inputEl"
HTML:
<input id="combo-1782-inputEl**" type="text" class="x-form-field x-form-required-field x-form-text x-trigger-noedit x-form-focus x-field-form-focus x-field-default-form-focus" autocomplete="off" name="type" readonly="readonly" aria-invalid="false" data-errorqtip="" style="width: 135px;">
id observed by inspecting the web page in normal chrome browser:
Then I used the above id in my java code(automation script) as below:
driver.findElement(By.id("combo-1782-inputEl")).click();
When I run the test > The google chrome browser opens automatically > The test gets successful till it meets the above line of code.
But, when it meets the above code line, the test failed Throwing the following exception:
class org.openqa.selenium.NoSuchElementException *
Then I inspected the same drop menu item in chrome web page opened (controlled) by automated test software, and found out that the "id" is different than the previous id mentioned in step 1.
The "id" in this case is: "combo-1781-inputEl"
id observed by inspecting the web page in chrome browser controlled by automated software:
As you can see, the number in the middle of the id has reduced from 1.
(1782-1 = 1781)
Same issue was found in the other drop menu items on the same web page.
What is the issue cause for this? How can I overcome this situation? Please help. :)
P.S. When I used "combo-1781-inputEl" ("id" from step 5) in my code, the test passed successfully.
driver.findElement(By.id("combo-1782-inputEl")).click(); //Test: failed
driver.findElement(By.id("combo-1781-inputEl")).click(); //Test: passed
I expected the test to be passed when I used the "id" I got in step 1 by inspecting the web page in normal chrome browser which is not controlled by automated software.
In order to provide you with the best answer, I'd need to see a section of HTML code for the web page, enough that I can see other attributes. When you have dynamic ID's you have two options:
Option 1: use an xpath that uses part of the ID that is constant, i.e.:
//*[starts-with(#id, 'combo-')]
might do the trick, but only if there are no other similar ID's.
Or perhaps:
//*[starts-with(#id, 'combo-') and ends-with(#id, '-inputEl')]
but that might still not be specific-enough. This is why seeing a section of your HTML would help.
Option 2: use other attributes instead of ID, i.e. class, text, or some other attribute.
//*[#name='FirstName']
for example. You can craft rather elaborate xpaths using combinations of attributes, and it will be fairly stable if you do it right. Sharpening your xpath creation skills will come in handy for things like this.
Use Xpath and WebDriverWait to handle dynamic element.Hope this will work.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#id, 'combo-')][#type='text']")));
As it is evident that the id attribute of the <input> tag changes dynamically e.g. "combo-1782-inputEl", "combo-1781-inputEl" and so on so you need to create a Dynamic Locator inducing WebDriverWait and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.x-form-field.x-form-required-field.x-form-text.x-trigger-noedit.x-form-focus.x-field-form-focus.x-field-default-form-focus[id$='-inputEl'][name='type']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#id, 'combo-') and #class='x-form-field x-form-required-field x-form-text x-trigger-noedit x-form-focus x-field-form-focus x-field-default-form-focus'][#name='type']"))).click();
I`m trying to use xpath type of parameter in my webdriver script.
However, although this parameter actually selects the same HTML tag:
By START_BUTTON = By.xpath(".//*[#name='MyName']");
as this one
By START_BUTTON = By.name("MyName");
I`m not being able to use the by.xpath in my testing script while the by.name is working fine
Not sure why I`m facing this issue. When tested in Firebug/FirePath the path:
.//*[#name='MyName'] - points exactly to the same A tag as By.name("MyName");
Thanks
My bad, I had the part of the code commented which actually caused this.
Sorry for bothering you.
Regards
Posting it for reference :
These are the locator strategies which helps to identify an element.
Following strategies are mainly used to identify an element :
By ID
By Name
By Class Name
By Tag Name
By Link Text
By Partial Link Text
By CSS
By XPath
I am trying to click on link which is generating runtime with some number at the end :
Html code is as below:-
<span class="float-left">
<a href="/Key/Lock/369">
How to use regular expression for xpath? please help.
It is working for exact no like:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/370']"));
I am trying below code with regular expression at last for any number but not working:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/\\d+']"));
Please help.
You don't need to go that far.
An XPath expression like this will work fine:
//a[starts-with(#href, '/Key/Lock')]
And even a normal CSS selector:
a[href^='/Key/Lock']
Note, this assumes it is always a number at the end, if it may not be, then it becomes much more difficult.