如何使用 selenium webdriver 处理 amazon.in 上的自动完成建议?

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

我正在尝试自动完成 amazon.in 上的自动完成建议。但与 google 搜索选项不同的是,当您右键单击建议时,您无法检查它。请帮我检查一下。非常感谢

我已验证以下代码,但返回的列表大小为零

截图:

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());
}

终于找到了。这并不容易(对我来说)。通常我所做的是打开开发工具栏并找到我认为下拉菜单所在的一般区域,然后开始将鼠标悬停在开发工具栏中,直到我看到下拉菜单亮起。这个被埋在 HTML.

的另一部分

我所看到的引导我去那里的是

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

当我将鼠标悬停在 div.nav-fill 上时,我看到它是搜索框。然后我注意到 div#nav-iss-attach 是空的,并且 id 谈到附加,这让我相信它是一个附加点(并且它靠近搜索框)。我在 HTML 中搜索了 nav-iss-attach,当我没有找到任何东西时,我开始扫描 HTML 中的相关内容。然后我 运行 穿过 div#nav-flyout-iss-anchor(它引用 iss),打开它,开始悬停,就是这样。此时您可以开始展开每个子元素并查看不同的建议以及 HTML 的构建方式。

以下是 "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>

请参考下面的代码,使用 Python 和 Selenium 测试亚马逊搜索引擎的 单字符输入 :- 只需创建一个包含 get-row-count、get_col_count、read-data 和 write_data 方法的 XLUtils 模块。 导入所有模块,您将在 SingleCharSearchData.xlx

中获得建议数量和结果列

尽情享受吧!!!!

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")

显然,我们可以对此进行更多优化,如果我这样做,我也会 post 该代码。

谢谢 戈文德·库马尔

只需 select 调试器上的源选项卡,执行搜索并弹出建议。看到建议后按 F8。此操作将冻结网页,您可以轻松检查它们。要再次恢复页面,您可以使用 F8 继续。 有关详细信息,请参阅所附图片。