Locating button with xpath - JAVA / Selenium 2 in Chrome - java

Code behind button:
<input class=”btn” id=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ name=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ onclick=”window.open(‘/apex/newscenario?Opportunity__c=006f00000072n8hAAA’,’_top’, 1);;A4J.AJAX.Submit(‘mypage:formid’,event,{‘similarityGroupingId’:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′,’parameters’:{‘mypage:formid:relatedScenaiosListId:j_id27:j_id28′:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′} } );return false;” value=”New” type=”button”>
I did a right click from the Inspect Element view and saw I could copy the Xpath and found it was :
//*[#id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]
Note the *.
I tired:
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();
and
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][#value='New']“));
txtnew.click();
but neither worked.
I’m curious about the *, if that should be part of my Xpath statement?

If it's not necessary for you to use xpath, use searching by id, or cssSelectors to find your element. E.g.
//if you have only one element with class btn you can use this selector
//if element placed in parent (and this can identify element much more) add selector to
//parent before .btn
WebElement txtnew = driver.findElement(By.Css(".btn"));
//or
WebElement txtnew = driver.findElement(By.Css("input[value='New']"));
//or if id not generated automatically
WebElement txtnew = driver.findElement(By.Css("#mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
//or using By.Id
WebElement txtnew = driver.findElement(By.Id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
Any of them will work in specific situation. Choose one which more suitable for your sitution. Thanks.

Try this xpath:
input[#value='New'][#class='btn'][starts-with(#id, 'mypage:formid')]

The real issue, that I didn't realize, was that the control was in a different frame. Once I set the driver to look at the frame any number of xpath expressions worked.
` driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.switchTo().frame("066i0000004bNpx");
WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
txtNewbtn.click();`

Related

Selenium not able to input element in the form inside fieldset tag

I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.

org.openqa.selenium.ElementNotInteractableException while clicking on span element with attribute "unselectable=on"

What I am trying:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath("//span[text()='TrailVersion, Testing_Demo']"));
Option 1:
element.click();
Option 2:
Actions action = new Actions(driver);
action.click(element).build().perform();
action.moveToElement(element).click().build().perform();
{Giving exception "org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite." }
Option 3:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
I would like to highlight that span tag contains attribute <span unselectable = "on">
I have tried all the above 3 options but unfortunately, nothing is working. I have tried different Xpath's for the same element too but in vain. The element has no unique ID or class.
Can anyone please help me resolve the issue?
unSelectable attribute
The unSelectable attribute sets whether the selection process can start in an element's content or not. If the unSelectable attribute of an element is set to on, then the element is selectable only if the selection starts outside the contents of the element.
In Firefox, Google Chrome and Safari, the -moz-user-select and -webkit-user-select style properties are used for implementing similar functionality.
The difference between the unSelectable attribute and the -moz-user-select and -webkit-user-select style properties is that the -moz-user-select and -webkit-user-select style properties specify whether an element can be selected while the unSelectable attribute only specifies whether the selection process can start in an element's content or not. Another difference is that the unSelectable attribute is not inherited while the -moz-user-select and -webkit-user-select style properties are inherited. It means that the unSelectable attribute must be set on all non-selectable elements regardless of whether the unSelectable attribute is set on the parent element of a non-selectable element or not.
This usecase
The relevant HTML would have been helpful to construct a canonical answer. However if the element is a dynamic element or the website is Kendo UI based, then to click on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using WebDriverWait and xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))).click();
Using Actions and xpath:
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]")))).click().build().perform();
Using JavascriptExecutor and xpath:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))));
Reference
You can find a relevent detailed discussion in:
select kendo dropdown using selenium python

How to find nested elements by class in Selenium

I have a div inside a div inside another div. Most outer div class is "Big Div", inside it there is a div with class "Medium Div" and the most inner div class is "Small Div".
I'm able to see the div's classes when I press the F12 key and hover over the elements, however I can't find them using Selenium.
What am I doing wrong?
WebElement big = browser.findElement(By.cssSelector("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.cssSelector("//div[contains(#class,'Medium')"));
WebElement small = medium.findElement(By.cssSelector("//div[contains(#class,'Small'"));
Note: my classes contain white spaces, Selenium can't find any of the divs and I get the exception: "No Such element".
The syntax you have used that is not for cssSelector that for XPATH and you have missed parenthesis as well.
Try following xpath now.
WebElement big = browser.findElement(By.xpath("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.xpath(".//div[contains(#class,'Medium')]"));
WebElement small = medium.findElement(By.xpath(".//div[contains(#class,'Small')]"));
However you can do it in once like.
WebElement small = browser.findElement(By.xpath("//div[contains(#class,'Big')]//div[contains(#class,'Medium')]//div[contains(#class,'Small')]"));
I would like to add a few lines to the answer of #KunduK
WebElement small = browser.findElement(new ByChained(By.xpath("//div[contains(#class,'Big')]"),By.xpath("//div[contains(#class,'Medium')]"),By.xpath("//div[contains(#class,'Small')]")));
When selenium already gives a few extra implementations, then why not to use it. :-)
You can get more details from the below link:
How Selenium's ByChained class really works?
https://www.linkedin.com/pulse/selenium-classes-stabilize-ui-automation-code-durga-behera/
Brackets are missing in the locator:
WebElement big = browser.findElement(By.cssSelector("div[class*='Big']"));
WebElement medium = big.findElement(By.cssSelector("div[class*='Medium']"));
WebElement small = medium.findElement(By.cssSelector("div[class*='Small')]"));
There is syntactical errors in placing parenthesis and the locator type used.
Try below code,
WebElement big = browser.findElement(By.xpath("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.xpath("//div[contains(#class,'Medium')]"));
WebElement small = medium.findElement(By.xpath("//div[contains(#class,'Small')]"));

Trying to find text element in a span

I am coding a selenium webdriver test in Java. I need the test to click on a "Yes" button in chrome that does not have an id or name.
I cannot find the element to click on the button that only has "Yes" as its unique identifier. There is also a "No" button.
I have tried to find the WebElement using xpath, classname and I have tried findElements function. Nothing succeeds.
This is the HTML:
<span class="ui-btn-inner">
<span class="ui-btn-text popup-anchor-text">Yes</span>
</span>
I have tried:
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text']/span"));
WebElement yesBtn = browser.findElement(By.xpath("//span[.='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[contains(text(), 'Yes')]"));
WebElement yesBtn = browser.findElement(By.xpath("//button[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
WebElement yesBtn = browser.findElement(By.xpath("//div[#class='ui-btn-text popup-anchor-text' and span='Yes']"));
yesBtn.click();
List<WebElement> yesBtn = browser.findElements(By.className("ui-btn ui-shadow ui-btn-corner-all ui-btn-up-a"));
yesBtn.get(0).click();
Error message:
NoSuchElementException; no such element. Unable to locate element.
The correct XPath locator would be:
//span[text()='Yes']
Just in case you can go for normalize-space() function:
//span[normalize-space()='Yes']
If this doesn't help:
Make sure that the span doesn't belong to an iframe, otherwise you will have to switch to the iframe before attempting to locate the element.
driver.switchTo().frame("your-frame");
Make sure to use WebDriverWait class just in case the element is not immediately available so WebDriver would perform several find attempts with polling interval
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']")));
Try out //span[#class='ui-btn-inner']/descendant::span[contains(text(), 'Yes')]. It should help out.

webdriver classname with space using java

This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?
I'm doing driver.findElement(By.className("current time")).click(); The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.
Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.
<span>
<a class="current time" href="http://someurl/" onclick="s_objectID="http://someur/">url</a>
</span>
Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,
WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();
Edit#1 Based on html provided,
Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,
WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();
or this should work too,
WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();
You can also use linkText since the element is link. From the html you provided, the link text is 'url'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
You can also use By.partialLinkText("partial link text here");
You can also use xpath as:
WebElement element = driver.findElement(By.xpath("//a[#class='current time']"));
element.click();
OR,
WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:
WebElement element = driver.findElement(By.xpath("//a[contains(#class, 'current') and contains(#class, 'time')]"));
Whenever you found some space in the class name you need to switch to cssSelector Locator.
Convert a class name to cssSelector if it is having a space as below.
In your case it would be:
WebElement element = driver.findElement(By.cssSelector(".current.time"));
element.click();
PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.

Categories

Resources