Web自动化测试 六 ----- selector选择

news/2024/7/7 21:27:36

1、一般情况下都是先定位元素在选择

from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECdriver = Chrome()driver.get('http://www.baidu.com')setting_ele_xpath = '//a[@name="tj_settingicon" and @href="http://www.baidu.com/gaoji/preferences.html"]'wait = WebDriverWait(driver, 20)
e = wait.until(EC.visibility_of_element_located((By.XPATH, setting_ele_xpath)))
driver.find_element_by_xpath(setting_ele_xpath).click()locator = (By.XPATH, "//a[contains(text(), '高级搜索')]")
WebDriverWait(driver, 30).until(EC.element_to_be_clickable(locator)).click()# 通常先去定位selector,再去定位option
# wait = WebDriverWait(driver, 20)
# my_selector = wait.until(EC.element_to_be_clickable((By.NAME, 'ft')))
# my_selector.click()# 在源代码中已经加载出来了,可以直接去点击
wait = WebDriverWait(driver, 20)
my_option = wait.until(EC.element_to_be_clickable((By.XPATH, '//option[@value="ppt"]')))
my_option.click()

2、使用selector选择器

from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECdriver = Chrome()driver.get('http://www.baidu.com')def wait_clicl_element(driver, locator):wait = WebDriverWait(driver, 20)return wait.until(EC.element_to_be_clickable(locator))setting_ele_xpath = '//a[@name="tj_settingicon" and @href="http://www.baidu.com/gaoji/preferences.html"]'wait = WebDriverWait(driver, 20)
e = wait.until(EC.visibility_of_element_located((By.XPATH, setting_ele_xpath)))
driver.find_element_by_xpath(setting_ele_xpath).click()locator = (By.XPATH, "//a[contains(text(), '高级搜索')]")
WebDriverWait(driver, 30).until(EC.element_to_be_clickable(locator)).click()wait = WebDriverWait(driver, 20)
my_selector = wait.until(EC.element_to_be_clickable((By.NAME, 'ft')))
my_selector.click()# select选择有3种方法:索引值、value值、文本,点进源码去看
selector_obj = Select(my_selector)
selector_obj.select_by_value('pdf')
# 取消 selector_obj.deselect_by_value()print(selector_obj.options)
print(selector_obj.first_selected_option)

  

转载于:https://www.cnblogs.com/qyh0902/p/11223216.html


http://lihuaxi.xjx100.cn/news/249543.html

相关文章

两个FTP对传文件

2019独角兽企业重金招聘Python工程师标准>>> #!/bin/bash ftp -n<<! open hostname user username password binary cd /FTP_A_Directory lcd /tmp/tmpSave prompt mget * close bye ! ftp -n<<! open hostname user username password binary cd /…

漫谈Httpclient

引用地址&#xff1a; http://hc.apache.org/httpclient-3.x/ End of life The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the Apache HttpComponents project in its HttpClient andHttpCore modules, whic…

安装MariaDB

结果我还是成功安装了MariaDB&#xff0c;其实大部分时候系统的操作不会有什么问题的&#xff0c;只是有时候会遇到一些问题较折腾。 最简单的指南&#xff1a;https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7 根据stackoverflow网友的说法…

Annotation

在进行类或方法定义的时候&#xff0c;都可以使用一系列的Annotation&#xff08;public interface Annotation&#xff09;进行声明&#xff0c;如果想要获取这些Annotation的信息&#xff0c;可以直接通过反射来完成。在 java.lang.reflect 里面有一个AccessibleObject类&…

个人永久性免费-Excel催化剂功能第101波-批量替换功能(增加正则及高性能替换能力)...

数据处理无小事&#xff0c;正如没有人活在真空理想环境一下&#xff0c;在数据分析过程中&#xff0c;也没有那么真空理想化的数据源可以使用&#xff0c;数据处理占据数据分析的80%的时间&#xff0c;每一个小小的改善&#xff0c;获益都良多。Excel查找替换&#xff0c;有其…

纯JS导出excel(支持中文)

JsonExportExcel 简介 github EXAMPLE 使用方法 1.引入js (dist目录下JsonExportExcel.min.js) <script src"https://cuikangjie.github.io/JsonExportExcel/dist/JsonExportExcel.min.js"></script>npm install -save js-export-excelconst ExportJson…

[转]MCC(移动国家码)和 MNC(移动网络码)

From : http://blog.chinaunix.net/uid-20484604-id-1941290.html 国际移动用户识别码&#xff08;IMSI&#xff09; international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码。  从技术上讲&#xff0c;IMSI可以彻底解决国际漫游问题。但是由于…

[emuch.net]MatrixComputations(1-6)

matrixComputation转载于:https://www.cnblogs.com/stoneresearch/archive/2012/06/24/4336678.html