`
sg552
  • 浏览: 623271 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
47437293-94b9-3b04-9152-8971c7580049
白手起家学习使用Flex
浏览量:18800
社区版块
存档分类
最新评论

selenium 进阶1: CSS SELECTOR

阅读更多

selenium 不如JQUERY对CSS支持的那么好一直是我心中的痛。

比如:找到页面中,找到一组 <a>, 然后再找到[2].parent()这个dom, jQuery得心应手,selenium 之前则只能借助于晦涩难懂的 xpath.  >_<

而 selenium 大牛们都是推荐 CSS selector的。所以。。。我找了一下解决方案,发现selenium 虽然使用了sizzle (https://github.com/jquery/sizzle/wiki/Sizzle-Home) ,但是对某些selector还是不支持的,下面就是“不支持”的列表:

根据这个文章,

http://marakana.com/bookshelf/selenium_tutorial/locators.html

6.7. CSS

The CSS locator strategy uses CSS selectors to find the elements in the page. Selenium supports CSS 1 through 3 selectors syntax excepted CSS3 namespaces and the following:

pseudo-classes pseudo-elements

:nth-of-type

::first-line

:nth-last-of-type

::first-letter

:first-of-type

::selection

:last-of-type

::before

:only-of-type

::after

:visited


:hover


:active


:focus


:indeterminate


  • css=div[id="pancakes"] > button[value="Blueberry"] selects the button with its value property set at Blueberry if children of the pancakes div

PROS :

  • Much faster than XPath
  • Widely used
  • Provides a good balance between structure and attributes
  • Allows for selection of elements by their surrounding context

CONS :

  • They tend to be more complex and require a steeper learning curve
分享到:
评论

相关推荐

    selenium +Java安装与使用

    - **元素定位**: Selenium提供了多种定位网页元素的方法,如`By.id`, `By.name`, `By.className`, `By.xpath`, `By.cssSelector`等。学会这些定位方法是进行网页操作的基础。 - **元素交互**: 一旦找到元素,就...

    selenium基础,selenium基础知识,Python

    text = driver.find_element_by_css_selector('.class-name').text ``` 7. 最后,关闭浏览器: ```python driver.quit() ``` 在Python爬虫中,Selenium可以用于处理动态加载的内容,这些内容通常无法通过传统的...

    selenium api

    3. Locator Strategies:定位网页元素的方法,如By.id、By.name、By.xpath、By.cssSelector等,用于找到页面上特定的元素。 4. Wait and Timeouts:设置等待时间或超时,确保元素加载完成后再执行后续操作,避免因...

    Selenium+java进行web自动化.pdf

    3. **CSS选择器定位**:适用于CSS样式属性,如`driver.findElement(By.cssSelector("input[type='submit']"))`。 4. **其他定位方法**:还包括类名、标签名、链接文本等。 **六、进阶话题** 1. **页面对象模式**:...

    Selenium+Eclipse+Junit+TestNG自动化学习笔记

    - **尝试使用不同的定位策略**(如ID、name、class、CSS selector、XPath)。 - **使用 Firebug 和 XPathChecker 插件辅助定位元素**。 **3. 等待元素出现** - **利用 WebDriverWait 和 ExpectedConditions 类**: ...

    今日头条Ajax美女图片爬取 - 爬虫进阶实战(完整版)

    1. **安装Selenium**: 首先,你需要安装Selenium库。使用pip命令`pip install selenium`。同时,还需要下载对应浏览器的WebDriver,例如ChromeDriver,将其路径添加到系统环境变量中。 2. **使用Selenium打开网页**...

    python爬虫面试题.pdf

    - **CSS Selector:** 更简洁,易于理解和记忆,适用于简单的选择操作。 6. **分布式爬虫实现:** - 使用Scrapy Cluster等工具实现分布式部署。 - 利用消息队列(如RabbitMQ、Kafka)进行任务分配。 - 多台...

Global site tag (gtag.js) - Google Analytics