Есть ли способ сначала выполнить поиск по главному окну, если он не найден, а затем начать поиск внутри фреймов? - PullRequest
0 голосов
/ 05 апреля 2019

Требование: Bydefault, поиск webelement в главном окне, если найдено, выполнить действие, иначе найти webelement внутри iframes и выполнить требуемое действие

Селен 3,141

'''
WebElement el = driver.findElement(By.xpath("//*[contains(text(),'here')]"));
    boolean displayFlag = el.isDisplayed();
    if(displayFlag == true)
    {
    sysout("element available in main window")  
    el.click();
    }
    else 
    {
      for(int f=0;f<10;f++)
      {
          sysout("element available in frameset")  
          switchToFrame(frameName[f]);
          el.click();
          System.out.println("Webelement not displayed");
      }
    }
'''

Мой скрипт не работает в первой строке. Он пытается найти элемент в главном окне, но элемент фактически доступен в iframe.

Но требуется сначала выполнить поиск в главном окне, а затем перейти только к фреймам. Как обращаться с таким вариантом использования?

Любое предложение было бы полезно? Спасибо.

1 Ответ

4 голосов
/ 05 апреля 2019

Да, вы можете написать цикл для прохождения всех фреймов, если элемент отсутствует в главном окне. Реализация Java:

 if (driver.findElements(By.xpath("xpath goes here").size()==0){
     int size = driver.findElements(By.tagName("iframe")).size();
     for(int iFrameCounter=0; iFrameCounter<=size; iFrameCounter++){
        driver.switchTo().frame(iFrameCounter);
        if (driver.findElements(By.xpath("xpath goes here").size()>0){
            System.out.println("found the element in iframe:" + Integer.toString(iFrameCounter));
            // perform the actions on element here
        }
        driver.switchTo().defaultContent();
    }
 }

Реализация Python

# switching to parent window - added this to make sure always we check on the parent window first
driver.switch_to.default_content()

# check if the elment present in the parent window
if (len(driver.finds_element_by_xpath("xpath goes here"))==0):
    # get the number of iframes
    iframes = driver.find_elements_by_tag_name("iframe")
    # iterate through all iframes to find out which iframe the required element
    for iFrameNumber in iframes:
        # switching to iframe (based on counter)
        driver.switch_to.frame(iFrameNumber+1)
        # check if the element present in the iframe
        if len(driver.finds_element_by_xpath("xpath goes here")) > 0:
            print("found element in iframe :" + str(iFrameNumber+1))
            # perform the operation here
        driver.switch_to.default_content()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...