如何在类中选择class,div,tag? [英] How select class , div , tag in splinter?
问题描述
我试图了解裂片的功能,我试图在网上找到,但找不到关于裂片的实用示例的良好文档,因此我在这里提出了几个问题,以便对尝试学习裂片的初学者有所帮助:
I am trying to understand splinter functionalities, i tried to find on web but i couldn't found good documentation with pratical examples on splinter so i asked few questions here so that it will help beginner who are trying to learn splinter :
首先,我混淆了分裂中实际的css选择器,到处都有两种方法:
First i am confuse what is actual css selector in splinter , there are two method i see everywhere :
browser.find_by_css()
或
browser.find_by_css_selector()
它们之间有什么区别,为什么第二个不能在当前的碎片中工作?
What is the difference between them and why the second one is not working in current splinter ?
现在我的原始问题是如何选择任何类下的任何标签,如何选择任何id下的任何标签?
Now My original question is how to select any tag which is under any class , how to select any tag which is under any id ?
我试图找到,但是在如何在下拉菜单中选择选项值"的分支上找到了大多数stackoverflow问题,分支文档非常好,但是问题是它们没有足够的示例实用方法.
I tried to find but i found most of stackoverflow questions on a splinter on "how to select option values in dropdown" , And splinter documentation is very good but the problem is they don't have practical sufficient examples for their methods.
所以,如果我有这个html代码:
So if i have this html code :
<div class="medium-widget success-story-category">
<h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2>
<p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p>
<ul>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download & Installation</a></li>
<li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li>
</ul>
</div>
然后:
-
如何通过选择
class="medium-widget success-story-category"
How to select
<p>
tag data by selectingclass="medium-widget success-story-category"
second:如何选择第一个<li>
标记
second : How select "href" of first <li>
tag
third:如何在第一个<li></li>
third : How get text between first <li></li>
现在,如果有<class_name id="something">
这样的话:
Now if there is <class_name id="something">
like :
<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">
<ul class="navigation menu" role="menubar" aria-label="Main Navigation">
<li id="about" class="tier-1 element-1 with-supernav" aria-haspopup="true">
<a href="/about/" title="" class=" current_item selected selected">About</a>
-
现在如何使用find_by_css方法(不使用find_by_id)选择具有ID的
<nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">
如何使用find_by_css
How get
<a>
link using find_by_css推荐答案
在这里我将解释我的答案,以便对其他程序员有所帮助:
I have found my answer here i am going to explain so that it will help to other programmers :
第一件事
browser.find_by_css_selector()
无法正常工作,我使用了find_by_css方法,该方法非常有效,所以我更喜欢find_by_css
方法.First thing
browser.find_by_css_selector()
is not working and i used find_by_css method which worked perfectly so i preferfind_by_css
method.如何通过选择
class="medium-widget success-story-category"
How to select
<p>
tag data by selectingclass="medium-widget success-story-category"
我们可以选择以下格式的任何类:
We can select any class the format is :
div[class="class_name"]
或div[any_style_element="value"]
我们可以选择课程
class="medium-widget success-story-category"
经过div[class="medium-widget success-story-category"]
we can select class
class="medium-widget success-story-category"
bydiv[class="medium-widget success-story-category"]
我们可以通过
('div[class="medium-widget success-story-category"] p')
标签
我们还可以通过:
we can also find with :
find_h=browser.find_by_css('div[class="medium-widget success-story-category last"]:nth-child(2)')
或
当html是
`<div class="row"> <div class="medium-widget success-story-category"> <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Getting Started</h2> <p>Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages. The following pages are a useful first step to get on your way writing programs with Python!</p> <ul> <li><a href="https://wiki.python.org/moin/BeginnersGuide/Programmers">Beginner's Guide, Programmers</a></li> <li><a href="https://wiki.python.org/moin/BeginnersGuide/NonProgrammers">Beginner's Guide, Non-Programmers</a></li> <li><a href="https://wiki.python.org/moin/BeginnersGuide/Download">Beginner's Guide, Download & Installation</a></li> <li><a href="https://wiki.python.org/moin/BeginnersGuide/Examples">Code sample and snippets for Beginners</a></li> </ul> </div> <div class="medium-widget success-story-category last"> <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly & Easy to Learn</h2> <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p> <ul> <li><a href="/community/workshops/">Conferences and Workshops</a></li> <li><a href="http://docs.python.org">Python Documentation</a></li> <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li> </ul> </div> </div>`
通过使用:
`find_h=browser.find_by_css('div[class="row"]:nth-child(1) > div:nth-child(1) > p') for i in find_h: print(i.text)`
我们可以捕获类中的图像
we can capture image which is in a class by
('div[class="image_class_name"] img')
然后是result["href" or "src"]
示例:
假设我必须选择该图像,然后可以通过以下代码获取它:
suppose i have to select that image then i can get it by this code :
find_h=browser.find_by_css('h1[class="site-headline"] img') for i in find_h: print(i["src"])
下一个问题是如何选择
- 标签:我们可以使用nth-child(n)选择
- 标签:
Next question is how to select
- tag : we can select
- tag usng nth-child(n) :
所以,如果我有这个html代码:
So if i have this html code :
<div class="medium-widget success-story-category last"> <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly & Easy to Learn</h2> <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p> <ul> <li><a href="/community/workshops/">Conferences and Workshops</a></li> <li><a href="http://docs.python.org">Python Documentation</a></li> <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li> </ul> </div> <div class="medium-widget success-story-category last"> <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Friendly & Easy to Learn</h2> <p>The community hosts conferences and meetups, collaborates on code, and much more. Python's documentation will help you along the way, and the mailing lists will keep you in touch.</p> <ul> <li><a href="/community/workshops/">Conferences and Workshops</a></li> <li><a href="http://docs.python.org">Python Documentation</a></li> <li><a href="/community/lists">Mailing Lists</a> and <a href="/community/irc/">IRC channels</a></li> </ul> </div>
然后我们可以使用
div[class="medium-widget success-story-category last"]:nth-child(1) > ul > li:nth-child(2) > a
请记住,
div[class="medium-widget success-story-category last"]:nth-child(1)
中的nth-child(2)不会选择该类的第二个嵌套div,而是nth-child(2)选择第二个medium-widget success-story-category last
类(您可以看到有两个同名的类medium-widget success-story-category last
).Please remember nth-child(2) in
div[class="medium-widget success-story-category last"]:nth-child(1)
doesn't select second nested div of this class instead nth-child(2) select secondmedium-widget success-story-category last
class (as you can see there are two classes with same namemedium-widget success-story-category last
) .最后一个问题的最后答案:
Last answer of last question :
如果有
<class_name id="something">
:然后选择
class_name[id="something"]
这篇关于如何在类中选择class,div,tag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!