Python的web自动化学习(四)Selenium的显性等待(元素定位)

引言:

Selenium的显性等待,其常用的定位方法介绍,后面持续更细具体用法

示例如下:

<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">

ID定位

wait.until(EC.visibility_of_element_located((By.ID, 'kw')))

Name定位:

wait.until(EC.visibility_of_element_located((By.NAME, 'wd')))

Class定位:

wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 's_ipt')))

Tag定位:

wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'element_tag')))

Link Text定位:

wait.until(EC.visibility_of_element_located((By.LINK_TEXT, 'link_text')))

Partial Link Text定位: Link Text包含定位:

wait.until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, 'partial_link_text')))

CSS Selector定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'css_selector')))

XPath定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'xpath')))

层级定位:

parent_element = driver.find_element(By.XPATH, 'parent_xpath')
wait.until(EC.visibility_of_element_located((By.XPATH, 'child_xpath')), parent_element)

多个条件组合定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'xpath')) and EC.element_to_be_clickable((By.ID, 'element_id')))

标签属性定位:

wait.until(EC.visibility_of_element_located((By.XPATH, '//tag[@attribute="value"]')))

元素文本定位:

wait.until(EC.visibility_of_element_located((By.XPATH, '//*[text()="element_text"]')))

元素属性值包含定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute*="value"]')))

元素属性值以定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute^="value"]')))

元素属性值结尾定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute$="value"]')))

元素文本包含定位:

wait.until(EC.visibility_of_element_located((By.XPATH, '//*[contains(text(), "element_text")]')))

属性值正则匹配定位:

wait.until(EC.visibility_of_element_located((By.XPATH, '//*[matches(@attribute, "regex_pattern")]')))

子元素定位:

parent_element = driver.find_element(By.XPATH, 'parent_xpath')
wait.until(EC.visibility_of_element_located((By.XPATH, 'child_xpath')), parent_element)

元素索引定位:

wait.until(EC.visibility_of_element_located((By.XPATH, '(//tag)[index]')))

兄弟元素定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'preceding-sibling::tag')))

父元素定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'parent::tag')))

祖先元素定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'ancestor::tag')))

子元素索引定位:

wait.until(EC.visibility_of_element_located((By.XPATH, 'parent_xpath/tag[index]')))

元素属性值不等于定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute!="value"]')))

元素属性值包含多个定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute~="value1"][attribute~="value2"]')))

元素属性值开始定位:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '[attribute^="value"]')))

以上是常用selenium的基本定位的方法,后面持续更新具体结合实际操作用法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

经历一个春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值