亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法先在主窗口中搜索網絡,如果沒有找到,然后開始在iframes內部搜索?

有沒有辦法先在主窗口中搜索網絡,如果沒有找到,然后開始在iframes內部搜索?

弒天下 2022-09-22 10:09:42
要求:Bydefault,在主窗口上搜索網絡設備,如果發現執行操作,則在 iframe 內搜索網絡設備并執行所需的操作硒 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中可用。但要求是首先在主窗口中搜索,然后僅導航到 iframe。如何處理這樣的用例?任何建議會有所幫助嗎?謝謝。
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

是的,您可以編寫一個循環來遍歷所有 iframe,如果元素不存在于主窗口中。實現:


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

    }

 }

蟒蛇實現


# 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()


查看完整回答
反對 回復 2022-09-22
  • 1 回答
  • 0 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號