About the interview summary 11-selenium face questions about the interview summary 11-selenium face questions

Reprinted: https://www.cnblogs.com/yoyoketang/p/10174938.html

About the interview summary 11-selenium face questions

Foreword

Interview web automation is bound to ask selenium, selenium ask questions related to positioning is the most basic, fundamental also automated, so the interview is inseparable from the positioning of the elements.
Before seeing recruitment requirements which says "only copy and paste xpath would not cast a resume," explained the interviewer automation capabilities of the applicant's request can not stay on the copy and paste.
Again, automated want to learn, then, to keep in mind: three generations of poor record, copy ruined his life!

1. How to determine whether there is an element on a page?

This can be said to be bad to ask questions, and determine the elements present in three ways:

A method using try ... except ...

def is_element_exsist(driver, locator):
    ''' 判断元素是否存在,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' try: driver.find_element(*locator) return True except Exception as msg: print("元素%s找不到:%s" % (locator, msg)) return False if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist(driver, loc1))

Method two: The method defines a group of elements with elements

def is_element_exsist1(driver, locator):
    ''' 判断元素是否存在,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' eles = driver.find_elements(*locator) if len(eles) < 1: return False else: return True if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist1(driver, loc1))

(Highly recommended!) Method three: Combining WebDriverWait judgment and expected_conditions

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait def is_element_exsist2(driver, locator): ''' 结合WebDriverWait和expected_conditions判断元素是否存在, 每间隔1秒判断一次,30s超时,存在返回True,不存返回False :param locator: locator为元组类型,如("id", "yoyo") :return: bool值,True or False ''' try: WebDriverWait(driver, 30, 1).until(EC.presence_of_element_located(locator)) return True except: return False if __name__ == '__main__': loc1 = ("id", "yoyo") # 元素1 print(is_element_exsist2(driver, loc1))

2. How to improve the stability of the script

There are similar problems related to "use cases often occur during operation instability, that is to say by this time, next time there is no way through, and how to improve the stability of the use cases?"
"How to improve selenium script execution speed? " "
how to ensure the success rate of the Selenium operating elements? regardless of network load that is slow or fast. "

If you can locate an element, it is positioned less than two days today, as long as this page has not changed too, explained the positioning method is nothing problem.
Optimization direction: 1. Do not copy right xpath (galaxy that path, certainly unstable), write your own relative path, multi-node id to find
2. Locate no problem, the second is to wait for the factors that, sleep waiting as little as possible (affect the execution time)
driver.implicitly_wait(30)the wait do not use, do not think that is a good thing overall, some js fail to load when it will be waiting, and when the jump page can not be recognized
3. locate the reseal element method, combined and determining WebDriverWait expected_conditions element method, a positioning element method own package

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait def find(locator, timeout=30): '''定位元素,参数locator是元祖类型, 如("id", "yoyo")''' element = WebDriverWait(driver, timeout, 1).until(EC.presence_of_element_located(locator)) return element

3. How dynamic positioning element

There are two kinds of dynamic elements, the property is a dynamic, such as id is dynamic positioning time, then do not use it wants to locate id

<p id="yo" class="hello world">    
    <button id="yy_auto_1929292" name="heo" >登录</button> <br> </p>

For example, the above elements of this button, id is dynamic positioning method thousands and thousands of dead on why id, positioning can name,
even if this element attributes are dynamic, it's impossible to dynamically label it, then locate the parent element id = "yo" ah: .//*[@id='yo']/button

There is also a dynamic situation, that is the element in the top of the page for a while, for a while at the bottom, erratic dynamic elements, positioning method is the same, according to f12, according to the positioning element attributes (tag elements, the pace of property name is will not change, the only class attribute dynamic properties and styles)

4. How is positioned through the sub-elements parent element

The interviewer do like to engage in some of the less popular location to test job applicants, when I was also sent his life to this problem. Came back specifically checked the relevant information, found the location method

through which the parent element selenium, the positioning sub-element, the element can be found through the secondary positioning:. ele1 = driver.find_element_by_id ( "yoyo ") find_element_by_id ( "ziyuans")
But before looking for a parent element of this sub-element by thinking I did not Note too, in fact, selenium which provides the process

<p id="yo" class="hello world">
    <button id="yy_auto" name="heo" >登录</button> <br> </p>

虽然用parent方法定位到了父元素,但是无法获取元素属性,也不能操作,没搞懂有啥意义

另外一个思路,子元素定位父元素,可以通过xpath的语法直接定位:.//*[@name="heo"]/.. 两个点..就是代表父级元素了

5.如果截取某一个元素的图片,不要截取全部图片

可以参考之前写过的这篇:https://www.cnblogs.com/yoyoketang/p/7748693.html

# coding:utf-8
from selenium import webdriver
from PIL import Image
driver = webdriver.Chrome()
driver.get('http://www.baidu.com/')

driver.save_screenshot('button.png')
element = driver.find_element_by_id("su")
print(element.location)                # 打印元素坐标
print(element.size)                    # 打印元素大小 left = element.location['x'] top = element.location['y'] right = element.location['x'] + element.size['width'] bottom = element.location['y'] + element.size['height'] im = Image.open('button.png') im = im.crop((left, top, right, bottom)) im.save('button.png')

6.平常遇到过哪些问题?如何解决的

可以把平常遇到的元素定位的一些坑说下,然后说下为什么没定位到,比如动态id、有iframe、没加等待等因素
如何解决的--百度:上海-悠悠,上面都有解决办法

7.一个元素明明定位到了,点击无效(也没报错),如果解决?

使用js点击,selenium有时候点击元素是会失效

# js点击

js = 'document.getElementById("baidu").click()'
driver.execute_script(js)

Guess you like

Origin www.cnblogs.com/yuany66/p/11344929.html