Home > Software design >  How to loop through pages where some pages have "display:none' style?
How to loop through pages where some pages have "display:none' style?

Time:02-26

I have 3 pages inside a website where I want to get data in each div with class='accordion-group'. When I load the page with selenium and do:

accordions = browser.find_elements_by_class_name("accordion-group")
print(len(accordions) = 57)

I saw that 57 div class='accordion-group' are available. But when I try to do web scrapping it stops at 20th element because the 20 first "accordion-group" are loaded on page 1 with display block. And I have to click on other page to display other "accordion-group".

I have two questions :

  • How can I do to know exactly the number of div "accordion-group" in each page knowing that other pages have "display : none" style unless I click on one the page.
  • How to loop through these 3 pages and get all data inside "accordion-group" in each page?

HTML where I am focus is below. When I click on page two, page 1 and 3 have 'display: none' and page 2 'display: block' style. Only one page can have block style.

<div >
    <ul>
        <li><a href >1<\a>
    <\ul>
    <ul>
        <li><a href class>2<\a>
    <\ul>
    <ul>
        <li><a href class>3<\a>
    <\ul>
<\div>
<div  id='accordion-et'>
  <div class='page' id='page1' style="display: block">
      <div class='accordion-group'>
      <\div>
      <div class='accordion-group'>
      <\div>
      <div class='accordion-group'>
      <\div>
      ...
  <\div>
  <div class='page' id='page2' style="display: none">
      <div class='accordion-group'>
      <\div>
      <div class='accordion-group'>
      <\div>
      <div class='accordion-group'>
      <\div>
      ...
  <\div>
  <div class='page' id='page3' style="display: none">
      <div class='accordion-group'>
      <\div>
      ...
  <\div>
</div>

CodePudding user response:

Fetch elements using xpath //div[@class='page' and contains(@style, 'display: block')]/div[@class='accordion-group']

  • Related