+ Python Selenium entry

A, Selenium + Python environment to build and configure

1.1 selenium Introduction

selenium is a web automated testing tools, many students start learning Automation preferred selenium, compared to QTP because it has many advantages:

  • Free, but also no longer need to crack a big headache QTP
  • Compact, it's just a different language for a package, but rather the QTP need to download and install more than one program G's.
  • This is the most important thing, whether you're more familiar with C before, java, ruby, python, or are C #, you can complete the automated testing by selenium, and QTP only supports VBS
  • Multiplatform support: windows, linux, MAC, supports multiple browsers: ie, ff, safari, opera, chrome
  • Support for distributed execution of test cases, test cases can be distributed to perform different test machines, the equivalent function of dispensing machine.

The official document:

+ Python environment configuration 1.2 selenium

Precondition: You have installed Python development environment (recommended installation Python3.5 and above)

installation steps:

  1. Install the Selenium
    Win: pip install selenium
    Mac:pip3 install selenium

  2. Installation webdriver
    major browsers webdriver address can be found: https://docs.seleniumhq.org/download/
    Firefox: https://github.com/mozilla/geckodriver/releases/
    Chrome: https://sites.google.com /a/chromium.org/chromedriver/  or
    http://chromedriver.storage.googleapis.com/index.html
    IEs: http://selenium-release.storage.googleapis.com/index.html
    NOTE: webdriver correspond to the needs and the browser version and a version of the corresponding selenium

Webdriver version Supported version of Chrome
v2.41 v67-69
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
  1. webdriver installation path
    Win: 复制webdriver到Python安装目录下
    Mac:复制webdriver到/usr/local/bin目录下

Second, the basic element positioning and browser operations

2.1 Start your browser

2.1.1 common way to start

Start Chrome browser:

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

Start Firefox browser:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.baidu.com/')

Start IE browser:

from selenium import webdriver

browser = webdriver.Ie()
browser.get('http://www.baidu.com/')

2.1.2 Headless way to start

Headless Chrome Chrome is a browser interface without form, you can open the browser without the premise of using Chrome supports all features running your program. Compared to a modern browser, Headless Chrome is more convenient testing web applications, get shots of the site, do reptiles crawl information. Compared to earlier PhantomJS, SlimerJS etc., Headless Chrome browser is closer to the environment.

Headless Chrome on Chrome Version:
official documentation describes, mac and linux environment requires chrome version is 59 +, and the windows version of chrome requirement is 60+, while chromedriver requirements 2.30+ version.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
# 使用headless无界面浏览器模式
chrome_options.add_argument('--headless') //增加无界面选项
chrome_options.add_argument('--disable-gpu') //如果不加这个选项,有时定位会出现问题

# 启动浏览器,获取网页源代码
browser = webdriver.Chrome(chrome_options=chrome_options)
mainUrl = "https://www.taobao.com/"
browser.get(mainUrl)
print(f"browser text = {browser.page_source}")
browser.quit()

2.1.3 load configuration to launch a browser

Selenium operation browser is not loading any configuration, the following method is about Chrome configured to load:

With Chrome address bar Chrome: // Version / , see your "personal data path", then when the browser starts, call the configuration file, the code is as follows:

#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data') #设置成用户自己的数据目录
driver=webdriver.Chrome(chrome_options=option)

The method to load the Firefox configuration is somewhat different:

The upper right corner to open the Firefox Settings>? (Help)> Troubleshooting Information> Display folder, open the copy down the path on it

# coding=utf-8
from selenium import webdriver
# 配置文件地址
profile_directory = r'C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default'
# 加载配置配置
profile = webdriver.FirefoxProfile(profile_directory)
# 启动浏览器配置
driver = webdriver.Firefox(profile)

2.2 Positioning elements

Positioning the object should be the core of automated testing, in order to operate an object, you should first identify the object. An object is the same as a person, he will have a variety of features (attributes), than we can as a person's identity card number, name, or where he lived two streets, floors, house to find this person. So an object has a similar property, we can find these objects by this property.

webdriver offers a range of object positioning method, commonly used are the following:

  • id定位:find_element_by_id()
  • name定位:find_element_by_name()
  • class定位:find_element_by_class_name()
  • link定位:find_element_by_link_text()
  • partial link定位:find_element_by_partial_link_text()
  • tag定位:find_element_by_tag_name()
  • xpath定位:find_element_by_xpath()
  • css positioning: find_element_by_css_selector ()
#coding=utf-8
from selenium import webdriver
browser=webdriver.Firefox()
browser.get("http://www.baidu.com")
#########百度输入框的定位方式##########
#通过id方式定位
browser.find_element_by_id("kw").send_keys("selenium")
#通过name方式定位
browser.find_element_by_name("wd").send_keys("selenium")
#通过tag name方式定位
browser.find_element_by_tag_name("input").send_keys("selenium")
#通过class name方式定位
browser.find_element_by_class_name("s_ipt").send_keys("selenium")
#通过CSS方式定位
browser.find_element_by_css_selector("#kw").send_keys("selenium")
#通过xpath方式定位
browser.find_element_by_xpath("//input[@id='kw']").send_keys("selenium")
############################################
browser.find_element_by_id("su").click()
time.sleep(3)
browser.quit()

2.2.1 class solution containing spaces:

During the actual positioning elements, class name is often found in combination with a plurality of class-based compound, the intermediate space separated. If the direct positioning error occurs, it can be handled in the following ways:

  • The only class property but there is a space, select only that one space on both sides
  • If the space is not the only class of spaced positioning by indexing
    self.driver.find_elements_by_class_name ( 'table-dragColumn') [0] .click ()
  • Css positioned by the process (space with '.' Instead)
#前面加(.)空格地方用点(.)来代替
self.driver.find_element_by_css_selector('.dtb-style-1.table-dragColumns').click()
#包含整个类
self.driver.find_element_by_css_selector('class="dtb-style-1 table-dragColumns').click()

Reference Code:

# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://mail.126.com/")
driver.implicitly_wait(20)
 
driver.switch_to.frame("x-URS-iframe")
# 方法一:取单个class属性
driver.find_element_by_class_name("dlemail").send_keys("yoyo")
driver.find_element_by_class_name("dlpwd").send_keys("12333")
 
# 方法二:定位一组取下标定位(乃下策)
driver.find_elements_by_class_name("j-inputtext")[0].send_keys("yoyo")
driver.find_elements_by_class_name("j-inputtext")[1].send_keys("12333")
 
# 方法三:css定位
driver.find_element_by_css_selector(".j-inputtext.dlemail").send_keys("yoyo")
driver.find_element_by_css_selector(".j-inputtext.dlpwd").send_keys("123")
 
# 方法四:取单个class属性也是可以的
driver.find_element_by_css_selector(".dlemail").send_keys("yoyo")
driver.find_element_by_css_selector(".dlpwd").send_keys("123")
 
# 方法五:直接包含空格的CSS属性定位大法
driver.find_element_by_css_selector("[class='j-inputtext dlemail']").send_keys("yoyo")


2.3 selenium three kinds of standby mode

Sometimes in order to ensure the stability of the script to run, you need to add latency scripts.

2.3.1 forced to wait

The first one is the most simple and crude way is forced to wait for sleep (xx), the need to introduce "time" module, which is called forced to wait, no matter whether you load the browser finished, the program had to wait for three seconds, three seconds a to continue to execute the following code, as is useful for debugging, so sometimes you can wait in the code, but this is not always recommended to wait way too rigid, serious impact on program execution speed.

# -*- coding: utf-8 -*-
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('http://baidu.com')

time.sleep(3)  # 强制等待3秒再执行下一步

print(driver.current_url)
driver.quit()

2.3.2 recessive wait

The second approach is called hidden wait, the method can be conveniently achieved by adding intelligence to wait implicitly_wait (); implicitly_wait (30) usage should be better than time.sleep () smarter, the latter can only choose to wait for a fixed period of time the former can be smart to wait within a time frame.

# -*- coding: utf-8 -*-
from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(30)  # 隐性等待,最长等30秒
driver.get('http://baidu.com')

print(driver.current_url)
driver.quit()

Stealth is set to wait for a maximum waiting time, if the page load completed within the stipulated time, then the next step, otherwise wait until the deadline, then the next step. Note that there is a downside, that program will wait until the entire page load is complete, that is, under normal circumstances you see the browser tab bar that no longer turn a small circle, the next step will be, but sometimes you want a page element early in loaded, but because individual things like js particularly slow, I still have to wait until page fully completed before the next step, I want to wait after the elements I want to come out and how to do next? There are ways, which is another way to look at selenium waiting to offer - the dominant wait wait.
Of particular note are: hidden waiting period for the entire driver have to work, so long as the set once, I have seen people waiting as a recessive sleep with, have to look at where to go ...

2.3.3 dominant waiting

A third way is to wait for explicit, WebDriverWait, until the class with () and until_not () method, can be performed flexibly according to the judgment wait condition. It mainly means: xx seconds to look at every program, if the conditions are established, the next step, otherwise continue to wait until the maximum time expires, then throw TimeoutException.

WebDriverWait dominant class wait wait class module, which look under it and method parameters:

selenium.webdriver.support.wait.WebDriverWait(类)

init

driver: 传入WebDriver实例,即我们上例中的driver
timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。

until

method: 在等待期间,每隔一段时间(__init__中的poll_frequency)调用这个传入的方法,直到返回值不是False
message: 如果超时,抛出TimeoutException,将message传入异常

until_not

与until相反,until是当某元素出现或什么条件成立则继续执行,
until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。

Read the above content is basically very clear, call as follows:

WebDriverWait (driver, long timeout, call frequency, ignore the exception) .until (executable method, the information returned timeout)

It should be particularly noted that the executable methods or method parameters until until_not of many people was introduced to WebElement object, as follows:

WebDriverWait(driver, 10).until(driver.find_element_by_id('kw')) # 错误

This is the wrong usage of certain parameters here if you can call that this object must have call () method, otherwise it will throw an exception:

TypeError: 'xxx' object is not callable

Here, you can use a variety of conditions expected_conditions module provided in the selenium, may be used in is_displayed WebElement (), is_enabled (), ** is_selected () ** method, or a method for their encapsulation can.

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

base_url = "http://www.baidu.com"
driver = webdriver.Firefox()
driver.implicitly_wait(5)
'''隐式等待和显示等待都存在时,超时时间取二者中较大的'''
locator = (By.ID,'kw')
driver.get(base_url)

WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道"))
'''判断title,返回布尔值'''

WebDriverWait(driver,10).until(EC.title_contains(u"百度一下"))
'''判断title,返回布尔值'''

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
'''判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement'''

WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su')))
'''判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0'''

WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw')))
'''判断元素是否可见,如果可见就返回这个元素'''

WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav')))
'''判断是否至少有1个元素存在于dom树中,如果定位到就返回列表'''

WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav')))
'''判断是否至少有一个元素在页面中可见,如果定位到就返回列表'''

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u'设置'))
'''判断指定的元素中是否包含了预期的字符串,返回布尔值'''

WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下'))
'''判断指定元素的属性值中是否包含了预期的字符串,返回布尔值'''

#WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
'''判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False'''
#注意这里并没有一个frame可以切换进去

WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
'''判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素'''
#注意#swfEveryCookieWrap在此页面中是一个隐藏的元素

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click()
'''判断某个元素中是否可见并且是enable的,代表可点击'''
driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click()

#WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su')))
'''等待某个元素从dom树中移除'''
#这里没有找到合适的例子

WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))
'''判断某个元素是否被选中了,一般用在下拉列表'''

WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True))
'''判断某个元素的选中状态是否符合预期'''

WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True))
'''判断某个元素的选中状态是否符合预期'''
driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()

instance = WebDriverWait(driver,10).until(EC.alert_is_present())
'''判断页面上是否存在alert,如果有就切换到alert并返回alert的内容'''
print instance.text
instance.accept()

driver.close()

2.4 Browser Operation

2.4.1 browser maximize, minimize,

The browser maximized

browser.maximize_window()

Will minimize the browser display

browser.minimize_window()

2.4.2 Browser Settings window size

Set the browser width 480, high 800 displays

browser.set_window_size(480, 800)

2.4.3 browser forward and back

go ahead

browser.forword()

Retreat

browser.back()


2.5 Operating test object

Generally, webdriver more commonly used method of operation of an object has several following:

  • Click on the object click--
  • send_keys-- analog key input on the object
  • clear-- clear the contents of the object, if possible
  • submit-- submission object, if possible
  • text-- for text information acquiring element

2.6 keyboard events

To invoke the operation keyboard keys need to introduce packages:
from selenium.webdriver.common.keys import Keyscall button by send_keys ():
send_keys(Keys.TAB) # TAB
send_keys(Keys.ENTER) # 回车

Reference Code:

#coding=utf-8 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys #需要引入 keys 包
import os,time

driver = webdriver.Firefox() 
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud .kuaibo.com%2F")

time.sleep(3) 
driver.maximize_window() # 浏览器全屏显示

driver.find_element_by_id("user_name").clear() 
driver.find_element_by_id("user_name").send_keys("fnngj")

#tab 的定位相相于清除了密码框的默认提示信息,等同上面的 clear() 
driver.find_element_by_id("user_name").send_keys(Keys.TAB) 
time.sleep(3) 
driver.find_element_by_id("user_pwd").send_keys("123456")

#通过定位密码框,enter(回车)来代替登陆按钮
driver.find_element_by_id("user_pwd").send_keys(Keys.ENTER)

#也可定位登陆按钮,通过 enter(回车)代替 click() 
driver.find_element_by_id("login").send_keys(Keys.ENTER) 
time.sleep(3)

driver.quit()

Use a combination of keyboard keys:

#ctrl+a 全选输入框内容 
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
#ctrl+x 剪切输入框内容 
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')

2.7 mouse events

Mouse events typically include the right mouse button, double click, drag, move the mouse over an element, and so on.
Need to introduce ActionChains class.
Introduction of methods:
from selenium.webdriver.common.action_chains import ActionChains

ActionChains 常用方法:
perform()  执行所有ActionChains 中存储的行为;
context_click()  右击;
double_click()   双击;
drag_and_drop()  拖动;
move_to_element()  鼠标悬停。

Double click Example:

#定位到要双击的元素
 qqq =driver.find_element_by_xpath("xxx") 
#对定位到的元素执行鼠标双击操作 
 ActionChains(driver).double_click(qqq).perform()

Drag and drop Example:

#定位元素的原位置 
element = driver.find_element_by_name("source") 
#定位元素要移动到的目标位置 
target = driver.find_element_by_name("target")
#执行元素的移动操作 
ActionChains(driver).drag_and_drop(element, target).perform()

2.8 multistory frame / level positioning

Positioning element process often encounter problems Element not found, the emergence of the problem usually leads to the following factors:

  • Elements are positioned the right way
  • There is a page or embedded iframe window
  • Page Timeout

webdriver provides a switch_to_frame method, we can easily solve this problem.
usage:

#先找到到 ifrome1(id = f1)
browser.switch_to_frame("f1")

Similarly, if it is embedded in the window:
browser.switch_to_window("f1")


2.9 Expected Conditions resolve

Expected Conditions of Use There are two scenarios:

  • Directly in the assertion
  • And WebDriverWait with the use of dynamic wait for the page elements appear or disappear

Related methods:

  • title_is: Determine whether the current page's title exactly equal to the expected
  • title_contains: Determine whether the current page title contains the expected string
  • presence_of_element_located: To determine whether an element is added to the dom tree, does not mean that certain elements visible
  • visibility_of_element_located: Determining whether an element of non-visible visible representative of the hidden element, and the width and height of the element are not equal to 0
  • visibility_of: With the above methods do the same thing, except that the above method to pass locator, this method is a direct pass to the element positioned just fine
  • presence_of_all_elements_located: Determine whether there is at least one element is present in dom tree. For example, if there are elements of the class n pages are 'column-md-3', as long as there is an element exists, the method returns True
  • text_to_be_present_in_element: An element in determining whether the text contains the expected string
  • text_to_be_present_in_element_value: Determine the value attribute contains an element of the expected string
  • frame_to_be_available_and_switch_to_it: To determine whether the frame can switch into it, if you can, and switch into returns True, otherwise it returns False
  • invisibility_of_element_located: To determine whether an element not present in the dom tree or invisible
  • element_to_be_clickable: To determine whether an element is visible and enable the, so called clickable
  • staleness_of: An element such as removed from the dom tree, note that this method also returns True or False
  • element_to_be_selected: To determine whether an element is selected, generally used in the drop-down list
  • element_selection_state_to_be: Checked to determine whether an element in line with expectations
  • element_located_selection_state_to_be: The role of the above method with the same, but the above method to the incoming positioning element, and this method pass locator
  • alert_is_present: To determine whether there is a page alert, this is an old problem, many students will be asked to

Example:
Analyzing title: title_is (), title_contains ( )

  1. First import the module expected_conditions
  2. Since this module name is longer, so in order to facilitate follow-up calls, renamed the EC (a bit like a multi-table queries inside the database when renaming)
  3. After opening the blog home page to determine title, the result is True or False
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://baidu.com")
# 判断title完全等于
title = EC.title_is(u'百度')
print title(driver)

# 判断title包含
title1 = EC.title_contains(u'百度')
print title1(driver)

# 另外一种写法
r1 = EC.title_is(u'百度')(driver)
r2 = EC.title_contains(u'百度')(driver)
print r1
print r2

Three, Selenium Issue

3.1 Python Webdriver Exception Issue

webdriver various exceptions may occur in the course, we need to understand the exception and know how to exception handling.

abnormal description
WebDriverException All webdriver exception base class, when there is an abnormality does not belong to the following exception is thrown
InvalidSwitchToTargetException The following two exceptions parent, you want to switch is thrown when the target does not exist
NoSuchFrameException Throw when you want to cut a nonexistent frame with switch_to.frame ()
NoSuchWindowException Throw when you want to cut a nonexistent window with switch_to.window ()
NoSuchElementException Element is not present, is generally thrown by find_element and find_elements
NoSuchAttributeException When you get the general element properties that do not exist throw, pay attention to some of the properties in different browsers have different attribute names
StaleElementReferenceException Specified element out of date, now is not the DOM tree, and may have been deleted or refresh the page or iframe
UnexpectedAlertPresentException Thrown appeared alert unexpected obstacle to the implementation of the Directive
NoAlertPresentException When you want to get thrown alert, but not actually appear alert
InvalidElementStateException The following two parent exception is thrown when an element of the desired operating state can not be
ElementNotVisibleException Elements are present, but not visible, can not interact
ElementNotSelectableException When you want to throw a select element can not be selected
InvalidSelectorException General xpath syntax error when you throw this error
InvalidCookieDomainException When you throw in the cookie want to add non-current url of the domain
UnableToSetCookieException When the driver is thrown you can not add a cookie
TimeoutException When an instruction is not thrown for a sufficient time to complete
MoveTargetOutOfBoundsException Throws actions of the move operation, the target moves outside a window
UnexpectedTagNameException Getting thrown to the elements of the label does not meet the requirements, such as examples of Select, you pass a non-select elements when the label
ImeNotAvailableException IME is not supported when thrown here two anomalies are not common, ime engine is said to be only for Chinese / Japanese support when under linux
ImeActivationFailedException Throws failed to activate the input method
ErrorInResponseException Not common, may throw an error when the server end
RemoteDriverServerException Not common, it seems that in some cases the drive to start the browser when the failure will be reported this wrong

3.2 Xpath & Css positioning method Issue

description Xpath Css
Direct child elements //div/a div > a
Child or descendant of //div//a div a
Id to locate //div[@id='idValue']//a div#idValue a
Positioning with class //div[@class='classValue']//a div.classValue a
Brother sibling elements //ul/li[@class='first']/following- ul>li.first + li
Attributes //form/input[@name='username'] form input[name='username']
More properties //input[@name='continue' and input[name='continue'][type='button
4 sub-elements //ul[@id='list']/li[4] ul#list li:nth-child(4)
第1个子元素 //ul[@id='list']/li[1] ul#list li:first-child
最后1个子元素 //ul[@id='list']/li[last()] ul#list li:last-child
属性包含某字段 //div[contains(@title,'Title')] div[title*="Title"]
属性以某字段开头 //input[starts-with(@name,'user')] input[name^="user"]
属性以某字段结尾 //input[ends-with(@name,'name')] input[name$="name"]
text中包含某字段 //div[contains(text(), 'text')] 无法定位
元素有某属性 //div[@title] div[title]
父节点 //div/.. 无法定位
同级哥哥节点 //li/preceding-sibling::div[1] 无法定位

Tips

这里介绍一款在线代码美化工具,在线访问地址:
https://carbon.now.sh

另外如果使用Vscode的话,可以安装对应的插件进行快速在线美化。

  • 打开Vscode,在插件栏输入:carbon-now-sh
  • 点击安装
  • 点击重新加载即可安装
  • 按快捷键ALT+CMD+A(win系统下使用:ALT+WIN+A)即可

效果预览:

发布了7 篇原创文章 · 获赞 6 · 访问量 621

Guess you like

Origin blog.csdn.net/weixin_36273267/article/details/104069483