Why we need x-path for finding elements in selenium? - selenium

I am beginner and using xpath and other locators like name, ID and so on for finding web elements in Web page. Just want to know why we need locators to find out elements.
Thanks

The locators are used in order to interact with the web pages by uniquely identifying elements. However, it is considered best practice to add data attributes specific for the test automation to the web pages, such as data-testid in place of using xpath, css selectors, etc.

Locators are used in order to interact with the webelements available on the page.
For ex. If I want to enter any text in the input field, first I would have to find out location of that input field on the web page and then only I would be able to enter text into it.

Related

How can I determine the name of the website by scraping the webpage?

Suppose some webpage is opened in my browser and I scraped the source code using scrapy. How can I identify the name of the website? I mean it can be in any tag. How can I uniquely identify it?
You can look for the tag in the .
For example, one can use something like: response.xpath('//title')

How to identify Facebook timeline element to post something with Robot Framework?

Can you help me to identify element ID or any other locator of timeline composer in Facebook profile ?
I need this to use in Robot framework with selenium2library to post something on my wall.
I can log in to Facebook, navigate to profile, but I cant input text into timeline composer. I tried to use Click element before inserting text, but no success.
I am using "inspect element" in browsers/firebug add-on to identify elements.
In this case, unfortunately all locators I have tried giving errors like:
Element does not appear in 5 seconds
or
Element must be user editable in order to clear it
Non dynamic locator for FB timeline-composer has name "xhpc_message_text" (18.10.2016)
Input text name=xhpc_message_text test

How to find out broken links/ images throughout all the pages of a website using selenium WebDriver?

I can find broken inks/ images in any particular webpage. But I am not able to find it throughout all the pages using Selenium. I have gone through many blogs but didn't find any code working. It would be great help if anyone one of you could help me to fix this problem
Collect all the href attribute in your page using the 'a' and 'img' tagname in a list.
In java, iterate the loop,setup a HttpURLCOnnection for each url from the href list. Connect to it and check the response code. Google for logic and error codes responses.
If you want to check broken images for all the pages, you can use HTTPClient library to check status codes of the images on a page.
First try to find all images on each page by using Webdriver.
Below is the syntax:
List<WebElement> imagesList = driver.findElements(By.tagName("img"));
Below is the syntax to get links
List<WebElement> anchorTagsList = driver.findElements(By.tagName("a"));
Now you need to iterate through each image and verify response code with HttpStatus.
You can find example from here Find Broken / Invalid Images on a Page
You can find example from here Find Broken Links on a Page

How to retrieve all the img id in a Webdriver Selenium Java

In a Browser I have couple of img id's which shall dynamically change in every different selection of the release date.
Can you please guide me with a selenium web driver code which shall click the checkbox image, even if the image id changes over every different selection of the release date.
How to retrieve all the img id's in a web browser?
<img id="R1410ENDec14001001" class="child-img" src="../dyn/assets/checkbox_unchecked.png">
You can go with class or other properties if the id is dynamic like
//img[#class='child-img'] or
//img[contains(#src,'checkbox_unchecked.png')]
To retrieve all the image's which has id you can go with the following xpath
//img[#id]
Use findElements By xpath using the above one, and iterate over and get the id atribute of each element

Find a link inside iframe in webbrowser control in VB.net

I want to find a url webbrowser control inside iframe.
1) my webbrowsercontrol opena url
2)that url has one iframe inside it
3) That Iframe has a link which I want to grab programmatically using vb.net
At any point of time use webBrowser1.Url.ToString() to get the URL of the current open link.
You can get the html code of the open url by using webBrowser1.DocumentText. Once you have the html code use string manipulation to find the "iframe src" value.
This can be abit complicated as you migt not know how may iframes you need to handle.
As well there are some limitations for the FRAME elements according to HtmlWindow.WindowFrameElement Property
You cannot access a FRAME elements or the FRAME's document if the
FRAME is in a different zone than the FRAMESET that contains it. For a
full explanation, see About Cross-Frame Scripting and Security.
Actually, all you need to do is this...
Msgbox Webbrowser1.document.frames(0),getelementbyid("linkTagId").href
This will show you the href of the link, don't bother wasting time with string manipulation.
Of course, you can loop through the frames and links as well using the .length properties in a for loop.
Also, there are ways to bypass the cross-frame security issues since you are running the code in an exe, there are examples online, just search for "bypass cross-frame security webbrowser control" in google without the quotes.
If you need more help with these let me know as I can tell you how. Remember the cross frame stuff only need bypassing if the parent domain name and iframe domain name are different (not subdomains though, they can be different no problems).
Let me know mate :)