Selenium targeting elements highlighted

Selenium in debugging a script, sometimes too fast because of the operation or operations is not obvious and is not clear whether the correct positioning of the elements. We are available to add style by performing js to target the elements to highlight locate elements.

In Selenim Webdriver may be () performed by driver.exectue_scirpt JavaScript codes, the code is executed JavaScript support incoming parameters, or even can be passed to locate the elements, such as:
exectue_script('js脚本', 参数1,参数2,参数3...)use of arguments in a JavaScript script string [n] It represents the first of several parameters to match.

Sample code is as follows:

from selenium import webdriver
from time import sleep

STYLE = "background: green; border: 2px solid red;"  # 高亮的样式

def find(driver, by, expr):
    element = driver.find_element(by, expr)
    driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",element, STYLE)  
    return element

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')

find(driver, 'id', 'kw').send_keys("博客园 韩志超")
find(driver, 'id', 'su').click()

sleep(3)
driver.quit()

Code above,
by targeting only way to support the 'id', 'xpath', 'name', 'class name', 'link text', 'partial link text', 'css selector', expr an element corresponding locating Expression formula.
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",element, STYLE)
Alternatively to the positioning element to arguments [0], the replacement style STYLE arguments [1], and add a highlight for Yang style element.

Executing the results as shown below:
Selenium targeting elements highlighted

Guess you like

Origin www.cnblogs.com/superhin/p/11482318.html