How to check for hyperlink through Selenium Webdriver? - selenium

I'm working with Selenium Webdriver on Eclipse, and I want to access the hyperlink for forget password:
<p class="mt15 text-center fs-12 forgot-password-links">Forgot Password?</p>
I've tried using linkText, partialLinkTest as well as WebElement. Nothing seems to work so far. Any suggestions? Thanks in advance!

According to the HTML you provided that element can be located with the following XPath:
//a[#href='forgotpassword.htm']
or if you prefer cssSelector
a[href='forgotpassword.htm']

Related

Selenium WebDriver, works fine on laptop but not on desktop.WHY?

I'm using Selenium Webdriver latest version and run the test on Chrome v69.0.3497.100.
When I execute script in my laptop, It working fine and click on the element but on desktop, it find element but can't click.
This is HTML of element:
<div class='col-sm-1'>
<div style="position: relative;margin-top: 19px;left:14px;" class="material-switch">
<input id="checkHiddenDevice" name="checkHiddenDeviceOption" type="checkbox">
<input id="checkHiddenDevice" name="checkHiddenDeviceOption" type="checkbox">
<label for="checkHiddenDevice" class="label-primary">
::before
::after
</label>
</div>
</div>
This is the Xpath:
//div[#class='col-sm-1']
I've also tried some xpaths like //label[#for='checkHiddenDevice'] or find element by CSS but it doesn't work. only xpath //div[#class='col-sm-1'] is working but in laptop only.
Then I try on KatalonRecorder Tool. When I put my Xpath and run, test case passed because it finds element but didn't click? Then I try to find Xpath by Katalon tool then Katalon generate this Xpath:
xpath=(.//*[normalize-space(text()) and normalize-space(.)='#of devices'])[1]/following::label[1]
But with this xpath it also can't find element (or I don't know how to change it to right xpath in my code)
Does anyone was faced with such problem?
Yes.. Even i have faced this issue, this issue might occur when the browser zooming is more than 100%. just execute the same scripts with 90% of browser zoom

Xpath seem to be right when i use chrome but fails on selenium

I am trying to find the text box in the image below
My xpath using chrome is
div[#id='element.problem.short_description']
div[#class='col-xs-10 col-md-9 col-lg-8 form-field input_controls']
still it fails on selenium.
the code is here on the webpage. I Couldn't copy it for some reason, so attaching a screenshot of it
Because you're not looking at the text field.
All you need is //input[#id='problem.short_description']

Unable to click on href element

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);

How to find Element [For Selenium]

I am practicing Selenium using Java Eclipse. I want to be able to log into Twitter using Selenium. I don't see any element associated with the username and password fields though. Why is that are no elements? How can I access these fields?
Thanks!
You can use xpath as below. These expressions are working form me.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/login");
driver.findElement(By.xpath("//div[#class='signin-wrapper']//input[#name='session[username_or_email]']")).sendKeys("Yourusername");
driver.findElement(By.xpath("//div[#class='signin-wrapper']//input[#name='session[password]']")).sendKeys("YourPassword");
driver.findElement(By.xpath("//button[contains(text(), 'Log in')]")).click();
You can simply access those fields with XPath:
//input[#placeholder="Phone, email or username"]
//input[#placeholder="Password"]
Let me know if for some reason you cannot use these selectors or in case of any exceptions ocurred
Here's how to find the XPath:
In chrome, click Inspect on the element of interest. You will have the option of copying the XPath from there. Image attached for reference:
Please use the xpath as below. we can able to access that.
(//*[#name='session[username_or_email]'])[2]

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.