2020-05-14python+selenium

一、定位元素

八种webdriver的基本定位方式:

1.id定位:find_element_by_id(self, id_)

2.name定位:find_element_by_name(self, name)

3.class定位:find_element_by_class_name(self, name)

4.tag定位:find_element_by_tag_name(self, name)

5.link定位:find_element_by_link_text(self, link_text)

6.partial_link定位find_element_by_partial_link_text(self, link_text)

7.xpath定位:find_element_by_xpath(self, xpath)

8.css定位:find_element_by_css_selector(self, css_selector)

对应的八种复数定位:

1.id复数定位:find_elements_by_id()

2.name复数定位:find_elements_by_name()

3.class复数定位:find_elements_by_class_name()

4.tag复数定位:find_elements_by_tag_name()

5.link复数定位:find_elements_by_link_text()

6.partial_link复数定位:find_elements_by_partial_link_text()

7.xpath复数定位:find_elements_by_xpath()

8.css复数定位:find_elements_by_css_selector()

二、等待

包括:强制等待,隐式等待、显示等待

强制等待:sleep() 强制线程休眠

隐式等待:implicitly_wait(),全局等待,在设定的时间内页面加载完成后执行下一步,否则抛出异常,作用于整个driver周期,针对驱动程序每次执行命令的最大化执行时间也可以理解为超时时间

显示等待:WebDriverWait()针对某个特定元素设置等待时间。在设置的等待时间内,每隔一段时间检查元素是否存在,如果存在则执行下一步,如果在设置的等待时间内不存在则抛出异常。默认每隔0.5秒检查

WebDriverWait( driver,  timeout,  poll_frequency=0.5,  ignored_exceptions=None).until(method,msg=None)  

WebDriverWait(driver,  timeout,  poll_frequency=0.5,  ignored_exceptions=None).until_not(method,msg=None)

driver:浏览器驱动

timeout:超时时间

poll_frequency默认检查频率,默认0.5s

ignored_exceptions,超时抛出异常信息,默认为NoSuchElementExeception

例如:

from selenium.webdriver.common.byimport By

from selenium.webdriver.support.uiimport WebDriverWait

from selenium.webdriver.supportimport expected_conditions

WebDriverWait(self.brose,120).until(expected_conditions.presence_of_element_located((By.XPATH,"//a[text()='测速历史']")))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。