Unable to click on href element - selenium

I have element as below
<td>
247137
</td>
On main screen link appear as "247137" on which I need to click.
I tried as
driver.findElement(By.xpath("driver.findElement(By.xpath(".//*[#id='theCase']/tbody/tr/td[3]/a"))).click();
but it's not clicking the element and returning NoSuchElementException.
I tried various ways like till the element visible, JavascriptExecutor but no luck.

Following xpath may help you:
driver.findElement(By.xpath('//*[contains(text(),'247137')])
Hope it will help you.

Don't forget that selenium web driver supports linkText and partialLinkText so if the text is unique you may be able to use the examples below.
Here is a Java example of find by "linkText" then click:
driver.findElement(By.linkText("247137")).click();
Or you could also use "partialLinkText" then click:
driver.findElement(By.partialLinkText("247137")).click();

Thanks this resolved.
WebElement Clickusecaseid = driver.findElement(By.xpath(prop.getProperty("Clickusecaseid")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", Clickusecaseid);

Related

How to click on this button with Selenium but the id is not unique

<button value="1" class="_42ft _4jy0 layerConfirm uiOverlayButton _4jy3 _4jy1 selected _51sy" type="submit">Confirm</button>
The above is the html code for a button on Facebook page. I would like help in selenium web driver to click on this button but the id of the tag is changing on every refresh
Code on facebook website
Thank you all for your help but I have solved this one with help from you all people and trial and error.
Use ExplicitWait until visibility of your element and then perform click
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")));
driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']")).click();
Another solution can be if some javascript preventing you to click the button then use below JavascriptExecutor code and let me know if any issue
WebElement button = driver.findElement(By.xpath("//button[#type='submit'][normalize-space()='Confirm']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
You can use the fixed class names for this
driver.findElement(By.className("layerConfirm"));
Or combination
driver.findElement(By.cssSelector(".layerConfirm.uiOverlayButton"));
If the dynamic id has fixed part you can use partial id
// id=someid134313
driver.findElement(By.cssSelector("[id*='someid']"));
Or by the "Confirm" text
driver.findElement(By.xpath("//button[contains(., 'Confirm')]"));
As Facebook element's class and id are dynamic, you will have trouble automating further with Selenium.
In this case you can go with xpath
//button[#type='submit' and text()='Confirm']
or
//button[text()='Confirm']
You can use following xpath for your case:
driver.findElement(By.xpath("//*[contains(text(),'Confirm')]")).click();
Hope it will help you.

Selenium By.linkText issues

So i am automating few tests for this one page, and using method "linkText" for finding element does not work even though it is clearly there,selector is written properly,element is visibile etc. Using its xpath does work however.
Any similiar experiences , ideas what may be the cause?
[Update]
WebElement element = driver.findElement(By.linkText("Sign up/login"));
<li class="n15-menu-arrow pull-right">Sign up/login
I see, can you please try partialLinkText?
WebElement element = driver.findElement(By.partialLinkText("login"));
or
WebElement element = driver.findElement(By.partialLinkText("Sign"));
If you locate a wrong element, that means in your HTML there are other elements happen to share this partial link text in their attributes, in that case, I recommend using Css Selector.

How to identify button without Xpath in selenium

Code is below
<button type="submit" class="login-button">Login</button>
In selenium I tried below code:-
driver.findElement(By.classname("Login")).click();
please help me in this code without Xpath
Your classname is login-button not Login
driver.findElement(By.classname("login-button")).click();
You can use also partialLinkText
driver.findElement(By.partialLinkText("Login")).click();
partialLinkText is looking the Sub-String on HTML DOM
You can use also linkText
driver.findElement(By.linkText("Login")).click();
LinkText is looking the same String on HTML DOM
Using CSS-Selector
driver.findElement(By.cssSelector("button[class='login-button']")).click();
Hope it will help you :)
I always Prefer Cssselector rather than Xpath, it's up to the user to choose which they want and what they are comfortable with finding element.
The below link will be very useful if you want to know about CSSSELECTOR.
http://www.w3schools.com/cssref/css_selectors.asp
driver.findElement(By.cssSelector(".login-button")).click();
My suggestion would be Please inspect element and open console
$('.login-button')
Try this one until you get the required element you want. In that way you will be more flexible in getting the most required element.

DOM element is clicking in debuggin mode, but not in executing in Selenium WebDrvier

I have code to click on Radio button like this,
clickElement(By.xpath(properties.getProperty("radio.security.team.exists.outside.yes")), "RadioExistOutside");
clickElement is the generic method to click on element by xpath
I have taken this xpath and put it in FirePath, it found that element, inspected that element in command line and $p.click(); did this.. it is able to click on it... I think there is no problem with xpath...
I went to debugging mode and put debugging points over these lines, when I step over it, it is click on the element, but when I run the script, it is not able to click.
I thought that thread acceleration might be causing this and put a wait before it, but it didn't worked? please help me in this...
For xpath you need to share your HTML code so we can verify the xpath
While you can try with JavascriptExecutor to click your radio button
WebElement element= driver.findElement(By.xpath("YOUR Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Note:- feel free to change the locator in above code according to your convenience.
Hope it will help you :)

click on submit button not working in selenium webdriver

I trying to click on the create account button in registration form.
this is how the button locate in the html page:
<div id="submitContainer"><button type="submit" class="large"><span><strong> Create Account </strong></span></button></div>
this is the button xpath:
//*[#id="submitContainer"]/button/span/strong
the problem is that the button don't have id, he locate inside a div.
I try to use by id,xpath,css,name, but all of this not working:
driver.findElement(By.id("submitContainer")).click();
driver.findElement(By.xpath("//*[#id='submitContainer']/button/span/strong")).click();
driver.findElement(By.tagName("Create Account")).click();
driver.findElement(By.className("large")).click();
thanks!
In your examples, except for the last one, you are not targeting the button. Now your last example, should actually locate the button-element:
driver.findElement(By.className("large")).click();
Could you please post the error message you are getting?
Are there more than one element on the page with className "large"?
Make sure the button is in view window, if it is then try clicking on it. Try to wait for the element to load. There might be an issue with your element being loaded into DOM -
driver.wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#type='submit']"))).click();
Hope this helps.
If you want to use xpath, the correct syntax is
//button[#type='submit']
Use this line below:
Thread.sleep(3000);
I got the result once I used this one. Since some time we need to give some sleep time for the site to load fully to pull the Xpath.
You can use the linkText
driver.findElement(By.linkText("Create Account")).click();
Hope it will work for you.