How to use the DOM locators in Selenium - selenium

I've the HTML source like this,
<input type="image" onclick="return logSub();" src="/images/Login_submit.gif" width="105" height="33" border="0" />
Here there is no ID or NAME. So I can only locate this using image index (which is hard) or using the src tag? But I dont know how use the src tag?
Is that possible?

See my answer to a previous question here: selenium: Is it possible to use the regexp in selenium locators
Basically the dom= protocol allows you to use javascript to locate elements for Selenium.

or with css:
css=input[type=image], [src="/images/Login_submit.gif"]

Have you tried
//input[#src='/images/Login_submit.gif']

Try this locator:
//input[contains(#src, 'Login_submit.gif')].

Related

Not able to find xpath. Chropath message - This element might be inside iframe from different src. Currently ChroPath doesn't support for them

Below is the HTML snippet I am trying to get xpath. I have tried so many combination in chrome dev tools but no luck. Chropath message is that element might be from different src.
I am not sure what it means. Any help appreciated.
<input type="text" class="sn-global-typeahead-input -global-active-input" name="sncwsgs-
typeahead-input" id="sncwsgs-typeahead-input" placeholder="Search Global" autocomplete="off"
aria-label="Search" aria-hidden="false" aria-expanded="true" aria-activedescendant="sncwsgs-
typeahead-record-1-2" aria-autocomplete="both" aria-owns="sncwsgs-typeahead-sections" aria-
controls="sncwsgs-typeahead-sections" aria-describedby="sncwsgs-typeahead-instructions" aria-
haspopup="listbox" value="" role="combobox">
There's an id tag, so you can use the following for the XPath:
"//input[#id='sncwsgs-typeahead-input']"
That also translates to the following CSS Selector:
"input#sncwsgs-typeahead-input"
But if that's inside an iframe, you'll need to switch to the iframe first before you can interact with elements inside of it.
Learn about switching into iframes here: https://www.selenium.dev/documentation/webdriver/interactions/frames/
Example of switching into an iframe:
driver.switch_to.frame("iframe")

How to check for hyperlink through Selenium Webdriver?

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']

How to write dynamic xpath for img src

i am trying to automate couple of Selenium-TestNg scenarios from the website - http://ecommerce.saipratap.net/checkpersonaldetail.php
I was trying to click on the "continue "button, but it seems to be an image.
Below is the snippet of the code. How to write the xpath for the same?
tried using the xpath - //a[contains(#href,'checkoutshiping.php')], but it didnt work
<a href="checkoutshiping.php"> ==$0
<img src="images/continue.gif" border="0"
style="cursor:hand;"> ==$0
</a>
Are you sure the user is logged in? I see other set of tags when check the website.
Have you tried below xpath.
//*[.='Checkout']
Use - //a[#href = "checkoutshiping.ph"], its gonna find your button. I checked it on your website.
Here is example from your website

xpath: find element with attribute AND contains?

I am writing selenium tests, and I need to switch to an iframe with no id or name and which parent element contains variable id's (so not helpful. Also, the src attribute has variable data in it as well, so I can't target it directly like By.cssSelector("iframe[src='example']"). I need an xpath selector that targets the src, but also that uses contains. I am trying to learn how to build xpaths outside of Chrome's Copy XPath but I can't figure this one out. Thanks for your help! Here is the iframe html:
<iframe scrolling="auto"
src="/admin/catalog/manage_variants_in_product.jsp?productId=160502"
width="100%" height="100%" frameborder="no"
style="position:absolute; top:0px; left:0px;">
</iframe>
The "contains" CSS selector might help here:
iframe[src*=manage_variants_in_product]
FYI, there are also ^= and $= that mean "starts with" and "ends with" respectively.
The better way I would recommend to learn building xpath or csspath is Firepath add-on of Firefox
First install Firebug in your Firefox browse and then install Firepath.
There you will get the efficient way to get the xpath or evaluate the xpath build by yourself

Selenium object identification

I am using Selenium webdriver to test my application & i am facing difficulties in identifiying button on the same. the code snippet is like :
<input type="submit" onclick="return sign(this);" value="Login">
and its xpath is :
html/body/table/tbody/tr[2]/td/center/form/center/table/tbody/tr[3]/td/center/input[1]
Which object property to use and how?
You should not use that XPath.
I would hazard a guess that you used some sort of tool, whether it's Firebug or IDE, to generate that XPath. Stop that now!
XPath is fine to use, and can be used here, just not relying on the tools to generate it for you! That XPath is destined for failure!
You will need to provide more HTML, specifically around that button.
However, you should just be able to use something as simple as:
//input[#value='Login']
You can use the xpath, if that is really stable. I found that it is much easier to define id tags in the html elements and the use a By.id locator. Alternatively you can use css selectors, depending on the "uniqueness" of your button something like this could work:
By.cssSelector("input[value='Login']")