简体   繁体   English

有没有办法减少Selenium WebDriver中的元素定位时间?

[英]Is there any way to reduce element locating time in Selenium WebDriver?

I have one select city dropdown and there are around 8000 city presents. 我有一个精选的城市下拉菜单,大约有8000个城市礼物。 My usecase are: 我的用例是:

  1. Get the selected city name 获取所选城市名称
  2. If city name is not as expected then select the desired city 如果城市名称不符合预期,则选择所需的城市
  3. Now again get the city name to verify that desired city has been selected 现在再次获取城市名称以验证是否已选择所需的城市

My code is working fine but the issue is to complete these 3 steps it takes around 5-8 minutes. 我的代码工作正常,但问题是完成这3个步骤需要大约5-8分钟。 I know its due to the large set of city name available in dropdown 我知道它是由于下拉列表中有大量的城市名称

This is the dropdown HTML structure : 这是下拉HTML结构:

<div class="col-md-12">
  <label class="mmk-filter-control">Preferred Source City : </label>
     <div class="pull-right refe-link">
     <div class="mmk-filter-control mmk-select-filter pull-right mr5">
     <select id="ddlPrefferedSourceCity" class="form-control" name="ddlPrefferedSourceCity">
          <option value="-1">- Select -</option>
          <option value="A.S.Peta Bypass">A.S.Peta Bypass</option>
          <option value="aadsar">aadsar</option>
          <option value="aagariya">aagariya</option>
          <option value="aahur">aahur</option>
          <option value="aakadiya">aakadiya</option>
          <option value="Aala">Aala</option>
          <option value="Aanjangaon">Aanjangaon</option>
           .
           .
          around 8000 options

And code is: 代码是:

if(usersname.size()>0)
{
     Select s = new Select(preferredCity);
     Comman.wait.until(ExpectedConditions.invisibilityOf(loader));
     String cityName  = s.getFirstSelectedOption().getText();
     if(cityName.equals(preferredCityName))
     {
         LogWriter.logger.info("Preferred City is already Selected");
         TakeScreenshot.passedScreenShot();
     }
     else
     {
         Comman.wait.until(ExpectedConditions.invisibilityOf(loader));
         LogWriter.logger.info("Last Selected Preferred City is : " + s.getFirstSelectedOption().getText());
         TakeScreenshot.passedScreenShot();
         s.selectByVisibleText(preferredCityName);
         setPreferenceButton.click();
         Comman.wait.until(ExpectedConditions.invisibilityOf(loader));
         LogWriter.logger.info("New Selected Preferred City is : " + s.getFirstSelectedOption().getText());
         TakeScreenshot.passedScreenShot();
     }
}

Is there any way to overcome and make the test fast? 有没有办法克服并快速进行测试?

The issue is likely due to getFirstSelectedOption since the method sends the isSelected command for each option: 问题可能是由于getFirstSelectedOption因为该方法为每个选项发送了isSelected命令:

https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/Select.java#L93 https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/Select.java#L93

So instead of : 所以代替:

s.getFirstSelectedOption().getText()

, I would use a selector to get the first selected option: ,我会使用选择器来获取第一个选择的选项:

preferredCity.findElement(By.cssSelector("option[selected]")).getText()

Update 更新

The selected attribute is not updated in the DOM when the selection changes. 选择更改时,DOM中不会更新selected属性。 So an alternative would be to read the selectedIndex property on <select> : 所以另一种方法是读取<select>上的selectedIndex属性:

int selectedIndex = Integer.parseInt(preferredCity.getAttribute("selectedIndex"));

WebElement selectedOption = preferredCity.findElement(By.cssSelector(
    String.format("option:nth-child(%s)", selectedIndex + 1)));

String text = selectedOption.getText();

or with executeScript : 或者使用executeScript

String JS_GET_FIRST_SELECTED_OPTION = 
    "var e=arguments[0], i=e.selectedIndex; return i < 0 ? null : e.options[i];";

JavascriptExecutor jse = (JavascriptExecutor)driver;
WebElement selectedOption = (WebElement)jse.executeScript(JS_GET_FIRST_SELECTED_OPTION, preferredCity);

if (selectedOption == null)
  throw new NoSuchElementException("No options are selected");)

String text = selectedOption.getText();

While 8000 cities in a single dropdown is a questionable design, it might not be the whole issue. 虽然单个下拉列表中的8000个城市是一个值得怀疑的设计,但它可能不是整个问题。 I created a simple HTML file that contained 100,000 options and selected a value from that list and it was complete in 12s, including loading the entire page. 我创建了一个包含100,000个选项的简单HTML文件,并从该列表中选择了一个值,它在12秒内完成,包括加载整个页面。 Granted it wasn't hosted on the web. 当然,它没有在网络上托管。 Your internet connection and/or the site's speed may be factors. 您的互联网连接和/或网站的速度可能是因素。

I would look at a few things: 我会看几件事:

  1. You seem to be using WebDriverWait in Comman.wait (Common is misspelled, btw). 你似乎在Comman.wait使用WebDriverWait (Common拼写错误,顺便说一句)。 Are you also using an implicit wait? 你还在使用隐含的等待吗? If so, the docs state not to mix both. 如果是这样, 文档声明不要混合两者。

    WARNING: Do not mix implicit and explicit waits. 警告:不要混合隐式和显式等待。 Doing so can cause unpredictable wait times. 这样做会导致不可预测的等待时间。

    If you are, remove the implicit wait and see if that helps. 如果是,请删除隐式等待,看看是否有帮助。

  2. Next I would try getting some timings. 接下来我会尝试一些时间。 Add a timing between each of the steps to see where the long waits lie. 在每个步骤之间添加一个时间点,以查看长时间等待的位置。 With that info, you will have a better idea of where the delays are coming from and how to address them. 有了这些信息,您就可以更好地了解延迟的来源以及如何解决这些问题。

  3. You are waiting for the loader to be invisible more than I think is necessary. 你正在等待加载器比我认为必要的更多看不见。 It shouldn't matter but removing some of them might help, especially if you are using an implicit wait (see #1). 这应该没关系,但删除其中一些可能会有所帮助,特别是如果您使用隐式等待(请参阅#1)。 You wait for it after you grab the SELECT, which shouldn't be needed. 你抓住SELECT后等待它,这是不需要的。 You wait for it again in the first line of the else but you haven't done anything that should trigger a loader. 你在else的第一行再次等待它,但你没有做任何应该触发加载器的事情。 The only place that seems reasonable is the 3rd instance, after you select the item from the dropdown. 从下拉列表中选择项目后,唯一合理的地方是第3个实例。

  4. Have you tried selecting by value instead of by visible text? 您是否尝试过按值而不是通过可见文本进行选择? It may not matter but I'm not really sure how it finds visible text. 这可能没关系,但我不确定它是如何找到可见文本的。

  5. Another option is to avoid use of the Select class altogether. 另一个选择是完全避免使用Select类。 While it's a good practice to use it normally because it makes dealing with SELECT so much easier, in this case it may be contributing to the performance issue. 虽然正常使用它是一种很好的做法,因为它使得处理SELECT变得更加容易,在这种情况下,它可能会导致性能问题。

    One way to get around it may be to go after the option directly using a CSS selector, eg #ddlPrefferedSourceCity > option[value='" + preferredCityName + "'] . 绕过它的一种方法可能是直接使用CSS选择器来选择,例如#ddlPrefferedSourceCity > option[value='" + preferredCityName + "'] I tried this on my local machine and it was slightly faster... but it's a difference of 1.2s vs .6s. 我在我的本地机器上尝试了这个并且它稍微快了......但它相差1.2秒vs .6s。

I really don't know why it is taking time to get the dropdown value using getFirstSelectedOption 我真的不知道为什么花时间使用getFirstSelectedOption获取下拉值

I have tried JavascriptExecutor where i have populated value of the dropdown using jQuery command 我已经尝试过JavascriptExecutor ,其中我使用jQuery命令填充了下拉列表的值

public String getDropdownValue()
{
        JavascriptExecutor e = (JavascriptExecutor) driver;
        return (String) e.executeScript("return $('#ddlPrefferedSourceCity').val();");
}

Now I'm able to get the value even change the dropdown value and again find the selected value. 现在我能够获得值甚至更改下拉值并再次找到所选值。 And now the time is 5-8 second . 现在时间是5-8 second

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium WebDriver Java使用动态ID定位元素 - Selenium WebDriver Java locating an element with dynamic ID 在硒中定位元素 - Locating an Element in Selenium 在硒中定位href元素 - Locating href element in Selenium Selenium WebDriver:当使用WebDriver.findElement定位时,等待元素存在是不可能的 - Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible 除了click()和submit()方法之外还有其他方法可以选择/单击selenium Webdriver中的web元素吗? - Apart from click() and submit() method is there any other way to select/click a web element in selenium Webdriver? 在Selenium WebDriver中找不到元素时杀死NoSuchElementException或任何异常的最快方法 - Fastest way to kill NoSuchElementException or any Exception when element is not found in Selenium WebDriver 在中定位元素 <li> 在Java中使用Selenium Webdriver - Locating elements in <li> using selenium webdriver with java 在 Selenium Webdriver 中定位 CK 编辑器并向其发送文本 - Locating CK Editor and Sending text to it in Selenium Webdriver Selenium WebDriver-用于选择输入和onclick的cssselector - Selenium WebDriver - cssselector for locating input + onclick 在Selenium Webdriver中的复杂表中查找数据 - Locating data in a complex table in Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM