【问题标题】:How to search and enter data into the search field如何在搜索字段中搜索和输入数据
【发布时间】:2015-02-09 01:14:09
【问题描述】:

我的场景:

  1. 创建一个 ID
  2. 在第 1 步中创建的搜索字段中输入 ID。

如果 id 在当前屏幕上可见/可用的 id 列表中可用(如下截图),我可以获取 id,然后进入搜索字段。但是,如果 id 在当前屏幕上不可见/不可用,则脚本无法检索它。在这种情况下,我将不得不单击右箭头按钮前进到下一个列表,在那里搜索 id,然后在搜索字段中输入。

如果在第一个屏幕中可用,我下面的代码会获取 id。我确实尝试使用 if 语句,但它不起作用。非常感谢任何帮助!

我的脚本:

/*3. Enter the Site ID of the advertiser created in the previous step.*/

        //this finds the ANY <a> tag with the text supplied by the site_id
        WebElement siteId = driver.findElement(By.xpath("//a[.='" + site_id + "']"));
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']")));

        if(!(driver.findElement(By.xpath(PvtConstants.READ_SITES_IDS)).getText().contains("//a[.='" + site_id + "']"))){

//click on the Right-Arrow Button
            driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click();

          //get a text/copy of the site_id
            String getThePreviouslyCreatedsiteId = siteId.getText().trim();

            //insert the text into the Advanced Filters Search Field
            driver.findElement(By.id(PvtConstants.READ_SITES_ADVANCED_FILTER_SITE_SEARCH_FIELD)).sendKeys(getThePreviouslyCreatedsiteId);

用户界面截图:

【问题讨论】:

  • 你必须使用 findElements 来知道 Id 是否存在,而不是使用 if 条件。请观看此视频以获取详细说明。 youtube.com/watch?v=4xILWxRC9aA。如果您能提供您遇到的错误,那就太好了。
  • gettext 将如何包含您传递的 xpath?屏幕上似乎没有任何可见的 xpath

标签: selenium selenium-webdriver


【解决方案1】:

在这种情况下你可以做一件事......

得到号码。您获得的条目数(如此处 27)并将其除以 no。你必须去的分页。之后在一个循环中(就像这里它将是 3),使用给定的 site_id 通过 xpath 找到元素,如果它可用,它将打破循环,否则单击下一页。 代码会有点像这样:

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement siteId = null;
while(i!=pagination){ // pagination is no. of pages you need to scrap
    try{
        siteId = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[.='" + site_id + "']"))); // will check for element to be visible, if not found then timeout exception will be thrown
        break; // if found will break the loop
    } catch(TimeoutException toe){
          driver.findElement(By.xpath(".//*[@id='dataTableSite_wrapper']/div[3]/div[2]/span[3]")).click(); // clicking next arrow 
          i++; // incrementing counter
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多