zoukankan      html  css  js  c++  java
  • selenium项目中遇到的问题总结

    问题:在pycharm中运行用例能成功,在命令行运行提示找不到com包
    解决办法:添加一个PYTHONPATH的环境变量,值为工程目录的路径

    当要查找的文本前后有换行时,用如下方法解决
    //td[contains(text(),'保证金管理费')]

    xpath方式定位同级元素的前面几个元素
    els = self.driver.find_elements_by_xpath("//td[contains(text(),'保证金管理费')]/preceding-sibling::td[3]")
    xpath方式定位同级元素的后面几个元素
    el = self.driver.find_element_by_xpath("//td[text()='审核意见:']/following-sibling::td[1]/textarea")


    使用xpath方法定位不包含某个属性的标签
    如下定位的是input标签包含value属性但不包含disabled属性
    //input[@value='YH'][not(@disabled)]

    # 拉动滚动条到最后
    js = "var q=document.documentElement.scrollTop=100000"
    driver.execute_script(js)
    有些弹出框使用上面的方法无法滚动则使用下面的方法:
    target = driver.find_element_by_xpath("//th[text()='开始时间']")
    driver.execute_script("arguments[0].scrollIntoView();", target)

    执行jQuery去掉readonly属性

    js = "$('input[name=" + el_name[1] + "]').removeAttr('readonly')"

    self.driver.execute_script(js)

    打开多个页面时切换操作
    #获取当前窗口句柄
    handle1 = driver.current_window_handle
    #点击页面某个链接弹出第二个窗口
    driver.find_element_by_xpath("//a[text()='" + MyTestCase.loanNo + "']").click()
    #获取所有窗口的句柄
    handles = driver.window_handles
    handle2 = ""
    #切换窗口
    for handle in handles:
    if handle != handle1:
    driver.switch_to.window(handle)
    handle2 = driver.current_window_handle
    time.sleep(2)
    #点击第二个窗口中的链接弹出第三个窗口
    driver.find_element_by_xpath("//a[text()='编辑内审信息']").click()
    # 切换到内审信息编辑窗口
    #再次获取所有窗口句柄
    handles = driver.window_handles
    #切换到第三个窗口
    for handle in handles:
    if handle != handle1 and handle != handle2:
    driver.switch_to.window(handle)
    #注意: 操作完后要使用driver.close()关闭当前窗口
    #并且要driver.switch_to.window(handle2)才能在第二个窗口上继续操作

    注意: driver的创建需要写在setUpclass或者setUp方法里面,否则使用HTMLReport运行所有用例时会同时打开很多个窗口

  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/sprouts/p/8256903.html
Copyright © 2011-2022 走看看