python selenium - how to read/store a text on a webpage -
https://gyazo.com/6f590c16fc078a0cd90fa2b0d4343d3c can see want script read text "29 dage" how locate selenium. can see underclass="dbalisting listing lastlisting"
and 4th want python read. how shall that?
selenium provides function find_element_by_css_selector()
locates first element based on provided css selector. (there's multi-item variant, find_elements_by_css_selector()
, returns list of matching elements.)
text = driver.find_element_by_css_selector('.dbalisting.listing.lastlisting > td:nth-child(4) > span').text
css main way of styling html. css selectors how elements chosen different styling. these selector rules used other tools well-known way of selecting elements.
.
indicates class. (there #
ids, not used here; nothing element names, used td
, span
; , :
or ::
psuedo-selectors.) tells system want elements class.
>
relationship operator meaning "parent/child relationship".
(space
technically relationship operator meaning "ancestor/descendant relationship". it's used readability when adjacent other relationship operators. it's common in css not used relationship operator here.)
:nth-child()
pseudo-selector meaning "siblings these indexes". used above, means "4th sibling". basic form of operator :nth-child(an+b)
. an+b
(as in: a
times n
plus b
) formula indicating items out of collection of siblings selected a
, b
integers , default 0
. n
integer handled css selector engine; can considered "any integer". elements start on index 1. parts can dropped formula filled out defaults. long form of :nth-child(4)
:nth-child(0n+4)
. in english read like: "sibling elements index matches formula an+b
n
integer, a
[supplied integer] , b
[supplied integer]." can read more selector here , here.
altogether have instructs:
- get elements have class "dbalisting"
- ... , have class "listing"
- ... , have class "lastlisting"
- get children of these elements
- ...
td
elements - ... , fourth child of parent
- get children of elements
- ...
span
elements
css selectors cheatsheat has quick rundown of various selectors.
Comments
Post a Comment