Why I get only first element?
css first-of-type class
css apply to only first element
first-child css not working
I'm parsing this page
I pull out links from the
number2 classes. Further in the loop I go through each element of
number2 and try to get the results from the class
'center bold table-odds'. To do this, I try to find the
parents of each link, but the problem is that every time I get the result from the
first element (in this example it is 31:25)
<table class="table-main odds prediction-table" id="prediction-table-1"> <tbody> <tr class="odd"> <td rowspan="3" class="center status-text-won">W</td> <td rowspan="3" id="status-IwnElQet" class="table-time center datet t1570978800-6-1-0-0 ">Today<br>15:00</td> <td rowspan="3" colspan="1" class="table-participant"> <a class="number2" href="/handball/europe/challenge-cup/vogosca-sviesa-IwnElQet/#1X2;2">1X2</a> </td> <td rowspan="3" class="center bold table-odds">31:25</td> <td class="center table-odds result-ok"><a href="">1.50</a></td> </tr> <tr class="even"> <td rowspan="3" class="center status-text-lost">L</td> <td rowspan="3" id="status-0IZCD4u8" class="table-time center datet t1570978800-6-1-0-0 ">Today<br>15:00</td> <td rowspan="3" colspan="2" class="table-participant"> <a class="number2" href="/volleyball/italy/serie-a2-women/marignano-talmassons-0IZCD4u8/#ah;2;-14.50;3">AH -14.5 Points</a> </td> <td rowspan="3" class="center bold table-odds">3:1</td> <td class="center table-odds result-ok"><a href="">2.01</a></td> </tr> </tbody> </table>
odds = driver.find_elements_by_class_name('number2') for odd in odds: print(odd.get_attribute('href')) print(odd.find_element_by_xpath('../..').find_element_by_class_name('center bold table-odds').text)
Your way to do it:
odds = driver.find_elements_by_class_name('number2') for odd in odds: print(odd.get_attribute('href')) print(odd.find_element_by_xpath('./ancestor::tr').find_element_by_css_selector('.center.bold.table-odds').text) # or # print(odd.find_element_by_xpath('./ancestor::tr//td') # or # print(odd.find_element_by_xpath('./ancestor::tr//td[contains(@class,'bold')]')
rows = driver.find_element_by_css_selector('#prediction-table-1 > tbody > tr') for row in rows: print(row.find_element_by_css_selector('.number2').get_attribute('href')) print(row.find_element_by_css_selector('.center.bold.table-odds').text)
:first-of-type, Instead of giving it a class, we can use :first-of-type to select it: the power of :first-of-type : it targets a particular type of element in a particular There are only two elements in the first period: hydrogen and helium. We can explain why there is less elements in the first row in modern theories of atomic structure. This is because in quantum physics, this period fills up the 1s orbital. Period 1 elements follows the duet rule, they only need two electrons to complete their valence shell.
You have a typo
Make it plural to get them all. Read more here
CSS :first-child Selector, Select and style every <p> element that is the first child of its parent: The :first-child selector is used to select the specified selector, only if it is the first child of its Neptunium, element 93 on the periodic table of elements, was the first transuranium element to be produced synthetically and the first actinide series transuranium element to be discovered.
Since there is only one class with name "number2" you are getting only on element and your is iterating once only. odds = driver.find_elements_by_class_name('number2')
.first(), Given a jQuery object that represents a set of DOM elements, the .first() method constructs a new jQuery object from the first element in that set. Consider a page When the first element of a certain class is also the first of its type, the pseudo-class will work, but this happens only by coincidence. This behavior is demonstrated in Philip's answer. The moment you stick in an element of the same type before this element, the selector will fail. Taking the updated markup:
:first Selector, While this matches only a single element, :first-child can match more than one: One first select the elements using a pure CSS selector, then use .filter(":first") . The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).
Array.prototype.shift(), The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array. but that depends on each sublist having exactly 2 elements. If there are different numbers of elements then you could first do an sapplyto calculate the lengths, then compute the corresponding 1st element positions (see cumsum), then select those values from the unlisted list. But by that time the accepted answer is probably much simpler.
- It's not a typo. I do not need everything. I need to iterate separately, one for each element of
- If you run as find_elements_by_class_name a list is returned and you can get each item as you need them using 
- You did not understand the essence of my question. I get all the elements with the class
number2. Next, I need to get the result through the
parentof each such element (an element with the class
center bold ...). I get the
parent, but when I get the
result, I constantly get the result from the
first element. Try to reproduce my situation. I attached the link. It is necessary to observe just such logic as I described (because there are not so unambiguous situations)
- There is a 15 elements with class name "number2".