DrissionPage/docs/特性演示/与 selenium 代码对比.md
2021-12-10 19:02:42 +08:00

97 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

以下代码实现一模一样的功能,对比两者的代码量:
- 用显性等待方式查找第一个文本包含 some text 的元素。
```python
# 使用 selenium
element = WebDriverWait(driver).until(ec.presence_of_element_located((By.XPATH, '//*[contains(text(), "some text")]')))
# 使用 DrissionPage
element = page('some text')
```
- 跳转到第一个标签页
```python
# 使用 selenium
driver.switch_to.window(driver.window_handles[0])
# 使用 DrissionPage
page.to_tab(0)
```
- 按文本选择下拉列表
```python
# 使用 selenium
from selenium.webdriver.support.select import Select
select_element = Select(element)
select_element.select_by_visible_text('text')
# 使用 DrissionPage
element.select('text')
```
- 拖拽一个元素
```python
# 使用 selenium
ActionChains(driver).drag_and_drop(ele1, ele2).perform()
# 使用 DrissionPage
ele1.drag_to(ele2)
```
- 滚动窗口到底部(保持水平滚动条不变)
```python
# 使用 selenium
driver.execute_script("window.scrollTo(document.documentElement.scrollLeft, document.body.scrollHeight);")
# 使用 DrissionPage
page.scroll_to('bottom')
```
- 设置 headless 模式
```python
# 使用 selenium
options = webdriver.ChromeOptions()
options.add_argument("--headless")
# 使用 DrissionPage
set_headless()
```
- 获取伪元素内容
```python
# 使用 selenium
text = webdriver.execute_script('return window.getComputedStyle(arguments[0], "::after").getPropertyValue("content");', element)
# 使用 DrissionPage
text = element.after
```
- 获取 shadow-root
```python
# 使用 selenium
shadow_element = webdriver.execute_script('return arguments[0].shadowRoot', element)
# 使用 DrissionPage
shadow_element = element.shadow_root
```
- 用 xpath 直接获取属性或文本节点(返回文本)
```python
# 使用 selenium
相当复杂
# 使用 DrissionPage
class_name = element('xpath://div[@id="div_id"]/@class')
text = element('xpath://div[@id="div_id"]/text()[2]')
```