Selenium填坑筆記

1. IE沒法正常啓動javascript

  1)Security 三個設置成同樣的,包括是否啓用保護模式也要同樣css

  2)頁面縮放比例要設置爲100%html

2.Firefox啓動後爲中文環境,但但願爲英文環境java

  Firefox driver每次會啓動一個徹底乾淨的瀏覽器,profile爲空的。可更改profile爲當前profile,這樣不只使用的就是當前default的環境,同時也就可以啓動plug-innode

  profile = webdriver.FirefoxProfile(r'C:\Users\Samsung\AppData\Roaming\Mozilla\Firefox\Profiles\xpttkqws.default-1427164221316')
      self.browser = webdriver.Firefox(profile)jquery

3.webElement點擊沒有響應web

  使用js模擬點擊瀏覽器

  compose = self.find_element(self.user_browser, *self.__menu_Compose_loc)
        self.user_browser.execute_script('arguments[0].click();', compose)測試

4.經過Send_keys()實現文件上傳,Chrome正常但,IE只彈出選擇文件對話框ui

  將IEdriver更新爲2.53.1正常了,2.53.0出現問題,但以前的老版本也沒有問題。經此IEDriver各個版本之間仍是有必定差別

  好比:

    老版本(已不能確認版本號大概是15年版本)中須要self.user_browser.execute_script("arguments[0].style.opacity='1';", input_attach),以後才能對元素執行send_keys()要否則報錯

    ElementNotVisibleException: Message: Element is not displayed

    新版本不須要

 5. Firefox在48版本以前不須要使用單獨的webdriver, 但須要禁止Firefox的自動更新功能,不然Firefox更新後將致使瀏覽器不能正常啓動。

6. Debug是想要輸出html標籤內容 <span>Compose</span>, 使用webelement.get_attribute("innerText")  輸出Compose.

7. 使用HTMLRunner輸出report

    def run(self,testSuite = None): name_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S") file_name = self.report_dir + str(name_time) +"_result.html" fp = open(file_name, "wb") runner = HTMLTestRunner(stream=fp, title="TestResult", description="Test result of compose mail: ") print "**knoxPortal Msg > testControl > run : Run start.." runner.run(testSuite) fp.close() print "**knoxPortal Msg > testControl > run : Run successfully.."

 8. 使用WebElement.findElement(By by) 是查找element的子元素,可是使用xpath時卻會查找全局

List<WebElement> findElements(By by) Find all elements within the current context using the given mechanism.
When using xpath be aware that webdriver follows standard conventions:
a search prefixed with
"//" will search the entire document, not just the children of this current node.

Use ".//" to limit your search to the children of this WebElement.
This method is affected by the 'implicit wait' times in force at the time of execution.
When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.

 9.xpath查找不包含某個子元素的當前元素

<li id="lpm_40611_0">
    <a>
        <ul class="prtit_area clear_both">
                <li class="date">當天購</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>購物車</a>
    </p>
</li>
<li id="lpm_40611_1">
    <a>
        <ul class="prtit_area clear_both">
                <li class="new">新品</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>購物車</a>
    </p>
</li>
<li id="lpm_40611_2">
    <a>
        <ul class="prtit_area clear_both">
                <li class="new">新品</li>
        </ul>
    </a>
    <p class="btn_cart">
        <a>到貨通知</a>
    </p>
</li>
<li id="lpm_40611_3">
    <a>
        <ul class="prtit_area clear_both"> ::after </ul>
    </a>
    <p class="btn_cart">
        <a>當天購</a>
    </p>
</li>

如今須要查找沒有分析當天購標籤,可是有購物車標籤的購物車元素

//ul[@class='prtit_area clear_both' and not(*[@class='date'])]//ancestor::li[@id]//a[contains(text(),'購物車')]

//ul[@class='prtit_area clear_both' and (not(li[@class='date']) or not(li))]//ancestor::li[@id]//a[contains(text(),'購物車')]

對於以上兩個的區別not(li[@class="date"])會查找有li標籤可是class不爲date的ul標籤

 

10. selenium2library在使用JavaScript時是使用關鍵字 execute JavaScript ,這個關鍵字只支持輸入一個純javascript的list,而底層的selenium庫中咱們在使用的時候更經常使用的是使用executejavascript("arguments[0].ckicl();",weblement),因此我麼須要本身封裝一下這個關鍵字

#原關鍵字
def
execute_javascript(self, *code): js = self._get_javascript_to_execute(''.join(code)) self._info("Executing JavaScript:\n%s" % js) return self._current_browser().execute_script(js)

#自定義關鍵字
def execute_javascript_with_args(self, code,*args):
  return self._current_browser().execute_script(code,*args)
  固然selenium2library其實給出了一種解決方法,就是使用關鍵字assign Id to element 給element設置一個id值,這樣javascript就可以很好的找到元素了。document.findelementbyid("myid ").click(); 或者使用 jquery,  $("#myid").click();
  這裏須要使用到jquery的語法,因此須要注意jquery是可以使用css樣式進行定位的語法,因此在$("")的括號中,只可使用css的locator 例如$("#id") $(".class") 或者其餘複雜的語法結構
 
11.  在web自動化測試過程當中常常遇到有些元素的value或者text值是經過js動態生成的,好比說日期輸入框採用日期控件。這個時候可能selenium自己的gettext(), getvalue()方法都不能正確獲取到值,這個時候可使用js去解決,$("Id").val() 可以獲取到value值,$("Id").val("setvalue")可以set值,$("id").text()可以獲取text
 
12.  Javascript還常常用在點擊問題上,由於selenium的 點擊其實是點擊元素所在位置的中心點像素,因此當元素被遮擋時click()可能不報錯,可是也沒有生效。這個時候最有效的解決方案就是使用js去點擊。
  在進行兼容性測試的時候發現win10上一些點擊事件會不生效,使用js點擊可以解決大部分問題。
  But, js點擊並非萬能的,在某些特定狀況下,使用js點擊以後觸發所點擊元素的onlick()事件,而頁面上其餘一些相關的事件不能正確觸發(eg:點擊了郵件中的SentBox按鈕,郵件列表加載了SentBox中的郵件,可是SentBox所在的div並無顯示爲選中狀態)因此把全部點擊事件都寫成js並非一個好的選擇,優先使用原生的點擊事件,當原生點擊事件不生效時再考慮使用js點擊。
  須要使用js點擊的除了遮蓋外,元素在viewport以外(例如存在滾動條,只有滑動滾動條元素才能可見)也是常使用的場景。不過這種在viewpoint以外使用原生的點擊之時是會報錯的(element is not clickable)。固然除了使用js點擊外,此時還可使用js滾動滾動條來使元素可見後點擊
#Java 
public void scrollToView(WebElement e) throws Exception {   
    executeJS("window.scrollTo(0," + e.getLocation().y + ")");
    executeJS("arguments[0].scrollIntoView(true);", e);
}
相關文章
相關標籤/搜索