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

时间:2017-09-10 12:30:47

标签: java selenium-webdriver autocomplete automated-tests amazon

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

我已经修改了以下代码,但返回的列表大小为零

截图:

enter image description here

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

3 个答案:

答案 0 :(得分:0)

我终于找到了它。这对我来说并不容易。通常我所做的是打开开发工具栏,找到我认为下拉列表所在的一般区域,然后开始在开发工具栏中悬停,直到我看到下拉菜单亮起。这个被埋在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>

答案 1 :(得分:0)

请参阅以下代码,以使用Python和Selenium为Amazon搜索引擎测试单个字符输入: 只需创建一个XLUtils模块,其中包含get-row-count,get_col_count,read-data和write_data方法。 导入所有模块,您将在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")

显然,我们可以进一步优化它,如果我愿意,我也会发布该代码。

谢谢 戈文德·库玛(Govind Kumar)

答案 2 :(得分:0)

只需在调试器上选择“ sources”选项卡,执行搜索并弹出建议。看到建议后,请按F8。此操作将冻结网页,您可以轻松地对其进行检查。要再次恢复页面,可以使用F8继续。 请参阅附件中的图片以获取更多信息。

Image reference