简体   繁体   中英

How to handle auto complete suggestions on amazon.in using selenium webdriver?

I am trying to automate auto complete suggestions on amazon.in. But unlike google search options, when you right click on the suggestions , you are not able to inspect it . Please help me in inspecting it . Thanks a lot

I have trued the following code but the list size returned is zero

Screenshot:

在此处输入图片说明

driver.get("https://www.amazon.in");
driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("Watch");
Thread.sleep(9000);
List<WebElement> findElements = driver.findElements((By.xpath("//div[@id='srch_sggst']/div")));
System.out.println("Size is"+findElements.size());
for(WebElement a:findElements)
{
    System.out.println(a.getText());
}

I finally found it. It was not easy (for me). Usually what I do is open the dev toolbar and find the general area of where I think the dropdown will be and start hovering things in the dev toolbar until I see the dropdown light up. This one was buried in another part of the HTML.

What I saw that lead me there was

<div class="nav-fill">
  <div class="nav-search-field ">
    <input type="text" id="twotabsearchtextbox" value="" name="field-keywords" autocomplete="off" placeholder="" class="nav-input" tabindex="6">
  </div>
  <div id="nav-iss-attach"></div>
</div>

When I hovered div.nav-fill , I saw that it's the search box. Then I noticed that div#nav-iss-attach was empty and the id talked about attach which lead me to believe it was an attach point (and it's close to the search box). I searched the HTML for nav-iss-attach and when I didn't find anything, I started scanning the HTML for something related. Then I ran across div#nav-flyout-iss-anchor (it references iss ), opened that up, started hovering, and that was it. At that point you can start expanding each of the child elements and see the different suggestions and how the HTML is built.

Here are the suggestions for "Selenium"

<div id="nav-flyout-iss-anchor">
  <div id="nav-flyout-searchAjax" class="nav-issFlyout nav-flyout" style="display: none; position: absolute; top: 6px; left: 238.344px; width: 656px;">
    <div id="suggestions-template">
      <div id="suggestions">
        <div id="issDiv0" class="s-suggestion" data-alias="aps" data-keyword="selenium supplements" data-store="All Categories" data-issc="false" data-isfb="" data-type="a9-xcat" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> supplements</div>
        <div id="issDiv1" class="s-suggestion" data-alias="hpc" data-keyword="selenium supplements" data-store="Health &amp; Personal Care" data-issc="false" data-isfb="" data-type="a9-xcat" data-nid="" data-crid="A3XLQRY17RL9"> <span style="padding-left: 16pt" class="a-size-mini">in <span class="a-color-tertiary">Health &amp; Personal Care</span></span></div>
        <div id="issDiv2" class="s-suggestion" data-alias="aps" data-keyword="selenium testing" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> testing</div>
        <div id="issDiv3" class="s-suggestion" data-alias="aps" data-keyword="selenium tablets" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> tablets</div>
        <div id="issDiv4" class="s-suggestion" data-alias="aps" data-keyword="selenium webdriver" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> webdriver</div>
        <div id="issDiv5" class="s-suggestion" data-alias="aps" data-keyword="selenium cookbook" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> cookbook</div>
        <div id="issDiv6" class="s-suggestion" data-alias="aps" data-keyword="selenium interview questions" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> interview questions</div>
        <div id="issDiv7" class="s-suggestion" data-alias="aps" data-keyword="selenium design patterns and best practices" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> design patterns and best practices</div>
        <div id="issDiv8" class="s-suggestion" data-alias="aps" data-keyword="selenium plus" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> plus</div>
        <div id="issDiv9" class="s-suggestion" data-alias="aps" data-keyword="selenium java" data-store="" data-issc="false" data-isfb="" data-type="a9" data-nid="" data-crid="A3XLQRY17RL9"><span class="s-heavy">selenium</span> java</div>
      </div>
    </div>
  </div>
</div>

Please refer to the below code for testing single character entry for Amazon search engine using Python and Selenium :- Just Create a XLUtils module containing get-row-count, get_col_count, read-data and write_data methods in it. Import all the modules and you get the Number of Suggestions and Results columns in SingleCharSearchData.xlx

Enjoy!!!!

import unittest
from pageObjects.config import Configg
from pageObjects.loginPage import Login
from pageObjects.searchpage import Search
import time
from testcases import XLUtils


class SearchTest(unittest.TestCase):
    def test_single_char_search_suggestions(self):
        conf = Configg(self)
        conf.driver_setup()
        login = Login(conf.driver)
        login.get_url()
        search = Search(conf.driver)
        search.click_search_bar()

        path = "C://Users//user//Amazon//Test Data//SingCharSearchData.xlsx"
        row = XLUtils.get_row_count(path, "Sheet1")
        print(row)

        for r in range(2, row+1):
            all_suggestion = {}
            single_char = XLUtils.read_data(path, "Sheet1", r, 1)
            conf.driver.find_element_by_xpath\
                ("//*[@id='twotabsearchtextbox']").send_keys(single_char)
            time.sleep(2)
            suggestions = conf.driver.find_element_by_id("suggestions").text
            all_suggestion[single_char] = suggestions
            conf.driver.find_element_by_xpath("//* 
                [@id='twotabsearchtextbox']").clear()

            for key in all_suggestion:
                all_suggestion_list = all_suggestion[key].split('\n')
                all_suggestion_list_v2 = []
                for item in all_suggestion_list:
                    if item.startswith(key) and not item.startswith("in"):
                        all_suggestion_list_v2.append(item)
                if len(all_suggestion_list_v2) == 10 or len(all_suggestion_list_v2) < 
                    10:
                    print("Test Passed.")
                    XLUtils.write_data(path, "Sheet1", r, 2, 
                        len(all_suggestion_list_v2))
                    XLUtils.write_data(path, "Sheet1", r, 3, "Test Passed")
                else:
                    print("Test Failed.")
                    XLUtils.write_data(path, "Sheet1", r, 2, 
                        len(all_suggestion_list_v2))
                    XLUtils.write_data(path, "Sheet1", r, 3, "Test Failed")

Obviously, we can optimise this more and if i do i will post that code too.

Thanks Govind Kumar

Simply select sources tab on the debugger, perform your search and let the suggestions pop-up. Once you see the suggestions press F8. This action will freeze the web page and you can easily inspect them. To resume the page again you can use F8 to continue. Refer to the attached image for more info.

图片参考

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM