Not able to click on button with xpath and other selectors - selenium

i am new to selenium, please see the below html, i want to click on this button and whenever i refresh/open this page id got changed every time. please help me to locate.
Code is---
<a id="d1did8122585133" class="button-position button2-gr td" rel="nofollow" onclick="callUntillDefined('showEnquiryForm', 0, 50, [{'query': 'did=8122585133&modid=ETO&ctg=164&comp=Atmiya%20Manufacturing&city=Surat&state=&country=India&modrefid=8122585133&ss=Machines%20%26%20Equipments&mcat=&modreftype=2&zoom_img=http%3A%2F%2F3.imimg.com%2Fdata3%2FCX%2FKN%2FMY-11421697%2Ffully-automatic-paper-plate-making-machines-250x250.jpg&display_image=http%3A%2F%2F3.imimg.com%2Fdata3%2FCX%2FKN%2FMY-11421697%2Ffully-automatic-paper-plate-making-machines-250x250.jpg','company':'Atmiya Manufacturing','enqu...rmType':'senEnqform','titleZoomForm':'Fully Automatic Paper Plate Making Machines','R_glusr_id':'11421697','R_custtype_weight':'149','R_title':'Fully Automatic Paper Plate Making Machines','grp':'164','displayId':'8122585133','modid':'ETO','query_text': 'SELL - Fully Automatic Paper Plate Making Machines','btn_txt':'Send Enquiry','enq_sent_color':changeColorEnquirySent,'btn_color':'button-position button2-gr td' ,'enq_enquiry_sent':'Yes'}]);recordInboundLinkS(this, 'Trac-Reply Now', 'Listing', 'Pos1', 0);">Send Enquiry</a>

You create xpath like below:
//a[text()="Send Enquiry"]
//a[contains(#class, 'button-position')]
//a[contains(#id, 'd1did')]
css=.button-position.button2-gr.td

I would suggest you do a more detailed study over how one can locate web elements.
You could visit "http://www.qaautomation.net/?p=388".

Related

Unable to access the element in the frame

I am still dipping my toes in selenium , I am SAP guy working on Selenium automation POC. my requirement is to click on the drop down and select a value from the droplist.
I have extensively looked at the previous posts but could not find any answers. I have tried all the suggestions from the post but nothing seems to be working for me.
Please can you help me how to access this drop down value.
HTML code is attached in the pic along with the element that I am trying to access
My selenium code :
driver.switchTo().frame(driver.findElement(By.name("SMFrame")));
System.out.println("TExt" + driver.findElement(By.xpath("//div[#class='file-type']")).getText());
Error:
error -- > no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='file-type']"}
Image
Assuming you are correctly getting into the iframe
Right now you are selecting a div above the actual dropdown element (the select-dropdown tag). You may want to try changing your xpath to search for the tag like so:
//div[#class='file-dropdown']/select-dropdown
Also you should be creating a dropdown object like this. Then calling a method on it to select the dropdown option you are looking for:
Select dropdown = new Select(driver.findElement(By.xpath("//div[#class='file-dropdown']/select-dropdown");
System.out.println(dropdown.selectByVisibleText("text that you are looking for in the options"));
This is a good explanation of how to do it: https://www.javatpoint.com/selenium-webdriver-handling-drop-downs

Selenium C# - I'm unable to find an element on this page using any of the locators

This was just a random script I made to complete a quiz but I can't seem to access the final element. I want to select the element, click the element and then send some text to the element.
I have tried to access the input box by class name, CssSelector and by XPath.
The website is https://www.16personalities.com/free-personality-test
Here are the XPaths I have tried:
//*[contains(#class, 'email-wrapper')]
//div[contains(#placeholder, 'your#email.com')]
//div[#class="row request-info-wrapper"]
//*[#id='request - email']"
Any help is greatly appreciated as I'm new to the framework and would very much like to know what I'm not understanding about locators! Thank you!
EDIT:
I can't seem to target this element or any of its children:
You have selected wrong tag DIV.Try this following Xpath. All should work.
"//input[#id='request-email']"
Or
"//input[#name='email']"
Or
"//input[#placeholder='your#email.com']"
Your field has a (presently) unique ID of "request-email".
Thus you can simply use, as a CSS selector,
('#request-email')
Then, in you can simply tell Selenium to hit ENTER to save your data. Let me know if you need help doing that.

How to retrieve the label text as per the html provided?

Sometimes I failed to get the inner text of a web-element; recently working on thePersonal insurance, and failed to get inner text of label (web element). Here are the script and screenshot of webpage inspected:
WebElement detailPostCode =driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
System.out.println("postcode label text "+detailPostCode.getText());
Could any please help me understand the problem. Thank you for your kind concern.
As per the HTML you have shared the <label> with text as Postal code is a child node of the <div> tag with class attribute as q_codePostal.
What went wrong?
As per your code trial you have used:
WebElement detailPostCode = driver.findElement(By.xpath("//label[#for='q_codePostalDetailAU']"));
In this expression //label[#for='q_codePostalDetailAU'] will always refer to the descending <input> tag with id attribute as q_codePostalDetailAU. Hence your code trial didn't work.
Solution
As an alternative you can use the following solution:
WebElement detailPostCode = driver.findElement(By.cssSelector("div.q_codePostal>label"));
Can you try this xpath:
driver.findElement(By.xpath("//div[#class='q_codePostal']/label"));

How to identify a webelement in Selenium if it cannot be identified even with xpath

I am trying to identify a link (link text is 'Pearson eText') in Selenium. I have tried using By.linkText, By.xpath and even By.tagName but none of these seems to work. I was wondering if someone can let me know if there are more ways to identify an element. Following is the html code from the webpage. Thanks in advance!
<div class="container_12" style="margin-bottom:10px;">
<div class="grid_5" id="studlinks"><h3>Welcome to MasteringAandP</h3><p>Access all your Mastering course resources and assignments
</p><h3>Mastering Assignments</h3><p>Access Mastering homework assignments.</p><h3>Mastering Scores (for students)</h3><p>Access your results for all Mastering work you have completed. </p><h3>User Settings</h3><p>Manage your Pearson account information.</p><h3>Pearson eText</h3><p>Access your Pearson eText.</p><h3>Study Area</h3><p>Access the self Study Area to watch videos and animations, take practice quizzes and more. Your work in the Study Area does not report to your instructor's grade book. To complete instructor-assigned activities, go to Assignments.</p><h3>MasteringAandP Course Home</h3><p>Access your MasteringAandP course for additional content and assignments.</p></div>
<div class="grid_5" id="instlinks"></div>
</div>
There are several ways. One of the CSS selectors ways could be:
WebElement personETextLink = driver.findElement(By.cssSelector("a[href=\"javascript:launch('171')\"]"));
And, if you go for xpath it won't despair you.
WebElement personETextLink = driver.findElement(By.xpath("//*[#href=\"javascript:launch('171')\"]"));
Finding the Link element by xpath:
//a[text()='Pearson eText']
If you want to get the href, you can easily do:
//a[text()='Pearson eText']/#href

Selenium Command

When we use selenium command at that time command not find and attribute not get? See below command.
<table>
<tr><td>open</td><td>http://www.wikipedia.org/</td><td></td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>assertAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>language</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>search1</td></tr>
<tr><td>assertAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
</table>
I am using Selenium IDE 2.5.0 in Mozilla Firefox and Ubuntu.
Xpath //div[2]#class central-featured is invalid. Try changing it to //div[#class='central-featured']/#class if you mean to select a class.
You could also use assertElementPresent function instead of selecting attribute, if the whole point is to check that element exists, i.e.:
<tr><td>assertElementPresent</td><td>xpath=//div[#class='central-featured']</td><td></td></tr>
Much simpler that way.
Use xPaths in this case.
Use google chrome's built in developer tool for this
Place your cursor on the element
Press Ctrl+Shift+C
Click the Element
That clicked Element's code is highlighted in the short window on the bottom
Right-Click on highlighted code
Select Copy > Copy XPath
Here it is you have copied the xPath for that specific element. This is shown in the image:
Click to see how to copy xPath
The Xpath you have used in Invalid.
You can use xpath as follows and through this you can fins xpath of any object - just need to study the concept:
Here as we can see we want to search Google Search just by writing its xpath in console
So to find the Google Search button we have to write xpath like this
//span[#id='gbqfsa']
Once we hit enter it would bring
[ gbqfsa">​Google Search​​ ],
It shows that xpath for Google Search Button is correctly written
Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs
then we have to use function starts-with like this
//span[starts-with(#id,'gbqfs')]
and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky
[
gbqfsa">​Google Search​​
,
​I'm Feeling Lucky​​
]
So to find out the Google Search uniquely we need to complete id attribute to gbqfsa
“//span[starts-with(#id,'gbqfsa')]
and hit to enter and now it would reflect only
[
​Google Search​​
],
It proves that we have written right xpath for Google Search
In the same fashion we can use Contains function to find the Google Search button like this
here I have taken fsa from gbqfsa
//span[contains(#id,'fsa')]
hit enter and hopefully it will return
[
​Google Search​​
],
if there are multiple attributes then we can use:
//span[contains(#id,'fsa') and contains(#class, 'xyz')] hit enter and hopefully it will return
Information Source: Sumit Mittal Blog
You can use CssSelector as below
webDriver.findElements(By.cssSelector("div.central-featured")) // for more than 1 elements with same class
webDriver.findElement(By.cssSelector("div.central-featured")) // for 1 element