When you search for something in Google and get the results, it is not easy to locate the elements, using Selenium, since Google uses classes like
<div class="kno-ecr-pt kno-fb-ctx gsmt">
What are the best practices working with that?
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.Sign up to join this community
One of the main reasons Google and similar top ranking pages make their page elements dynamic is to hinder automation. Most of the attributes like id, class name e.t.c are dynamic and hence out of context here. However, their HTML structure remains almost always the same. That is why we need to make use of xpath and css locators since they rely more on the structure of pages. You need to find a structure for the page you are trying.
In your case, I assume you are trying to find the company name and website after searching for the company name.
Here's the xpath for both:
Company name = //div[@data-ved]/div[@lang]
Company website = //div[@data-ved]/div[@lang]
If you can specify more about the requirement, we might be able to help with more helpful answers. And it is always suggested to try first and post your findings or issues.
Basically you just have to adapt to the page of interest. What approach works best depends on what you're trying to achieve.
If you're looking for specific results, I'd suggest looking for link text or partial link text. This searches the text contained in the
<a> tag, not the URL in the
href attribute. If you want to look for the URL, use a CSS selector for the
href attribute's contents.
If the summary text is more reliable, grab the summary text of the results and look thru that. If you look at the structure of the page, you'll find that each result is wrapped in
<div class="g">, within which is
<div class="r"> containing the links and
<div class="s"> containing the summary text. You can use this structure to always select the nth result, or to go thru the text summaries and select the corresponding link.
Those are some ideas to get you unblocked.