Home > other >  How to iterate divs in selenium python
How to iterate divs in selenium python

Time:12-27

I want to iterate this html div

<div  ref="eCenterContainer" role="rowgroup" unselectable="on" style="width: 1550px; height: 118.4px;"><div role="row" row-index="0" aria-rowindex="4" row-id="RGB4075DC397C648815FEFFFF0629B28AF02C6D6A80V3DA701" comp-id="4579"  aria-selected="false" style="height: 29.6px; transform: translateY(0px); " aria-label="Press SPACE to select this row."><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="122" comp-id="4580" col-id="recordingUrl"  style="width: 90px; left: 0px;  "><div><a href="javascript:;" ><i ></i></a> <a href="javascript:;"  style="display: none;"><i ></i></a></div></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="123" comp-id="4581" col-id="campaignName"  style="width: 209px; left: 90px;  "><span><span title="Click to Filter" >Window Installation Inbound</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="124" comp-id="4582" col-id="publisherName"  style="width: 209px; left: 299px;  "><span><span title="Click to Filter" >Aef media group</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="125" comp-id="4583" col-id="inboundPhoneNumber"  style="width: 139px; left: 508px;  "><span><span title="" > 19104458082</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="126" comp-id="4584" col-id="number"  style="width: 139px; left: 647px;  "><span><span title="Click to Filter" > 18333361696</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="127" comp-id="4585" col-id="timeToCallInSeconds"  style="width: 70px; left: 786px;  "><span><span title="Click to Filter" >00:00:00</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="128" comp-id="4586" col-id="isDuplicate"  style="width: 70px; left: 856px;  "><span><span title="Click to Filter" >Yes</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="129" comp-id="4587" col-id="endCallSource"  style="width: 56px; left: 926px;  "><span><span title="Caller" ></span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="130" comp-id="4588" col-id="timeToConnectInSeconds"  style="width: 70px; left: 982px;  "><span>00:00:05</span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="131" comp-id="4589" col-id="targetName"  style="width: 209px; left: 1052px;  "><span><span title="Click to Filter" >Window Ever</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="132" comp-id="4590" col-id="conversionAmount"  style="width: 69px; left: 1261px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="133" comp-id="4591" col-id="payoutAmount"  style="width: 70px; left: 1330px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="134" comp-id="4592" col-id="callLengthInSeconds"  style="width: 70px; left: 1400px;  "><span><span title="Click to Filter" >00:01:04</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="135" comp-id="4593" col-id="action"  style="width: 80px; left: 1470px;  "><span><div>
              <a title="Block Number" >
                    <i ></i>
                  </a>

              <a title="Add Tag Annotation" >
                <i ></i>
              </a>

              <a title="Adjust Call Payments" >
                  <i ></i>
                </a>
            </div></span></div></div><div role="row" row-index="1" aria-rowindex="5" row-id="RGBDA27E840095A2D3399CFA05C93C0A16964830F8FV3EHS01" comp-id="4595"  aria-selected="false" style="height: 29.6px; transform: translateY(29.6px); " aria-label="Press SPACE to select this row."><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="122" comp-id="4596" col-id="recordingUrl"  style="width: 90px; left: 0px;  "><div><a href="javascript:;" ><i ></i></a> <a href="javascript:;"  style="display: none;"><i ></i></a></div></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="123" comp-id="4597" col-id="campaignName"  style="width: 209px; left: 90px;  "><span><span title="Click to Filter" >Window Installation Inbound</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="124" comp-id="4598" col-id="publisherName"  style="width: 209px; left: 299px;  "><span><span title="Click to Filter" >Aef media group</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="125" comp-id="4599" col-id="inboundPhoneNumber"  style="width: 139px; left: 508px;  "><span><span title="" > 17137732947</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="126" comp-id="4600" col-id="number"  style="width: 139px; left: 647px;  "><span><span title="Click to Filter" > 18333361696</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="127" comp-id="4601" col-id="timeToCallInSeconds"  style="width: 70px; left: 786px;  "><span><span title="Click to Filter" >00:00:00</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="128" comp-id="4602" col-id="isDuplicate"  style="width: 70px; left: 856px;  "><span><span title="Click to Filter" >Yes</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="129" comp-id="4603" col-id="endCallSource"  style="width: 56px; left: 926px;  "><span><span title="Caller" ></span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="130" comp-id="4604" col-id="timeToConnectInSeconds"  style="width: 70px; left: 982px;  "><span>00:00:05</span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="131" comp-id="4605" col-id="targetName"  style="width: 209px; left: 1052px;  "><span><span title="Click to Filter" >Window Ever</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="132" comp-id="4606" col-id="conversionAmount"  style="width: 69px; left: 1261px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="133" comp-id="4607" col-id="payoutAmount"  style="width: 70px; left: 1330px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="134" comp-id="4608" col-id="callLengthInSeconds"  style="width: 70px; left: 1400px;  "><span><span title="Click to Filter" >00:00:11</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="135" comp-id="4609" col-id="action"  style="width: 80px; left: 1470px;  "><span><div>
              <a title="Block Number" >
                    <i ></i>
                  </a>

              <a title="Add Tag Annotation" >
                <i ></i>
              </a>

              <a title="Adjust Call Payments" >
                  <i ></i>
                </a>
            </div></span></div></div><div role="row" row-index="2" aria-rowindex="6" row-id="RGB11E56B7E626FA1C7A4507B513239AFBE495206CEV3PCS01" comp-id="4611"  aria-selected="false" style="height: 29.6px; transform: translateY(59.2px); " aria-label="Press SPACE to select this row."><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="122" comp-id="4612" col-id="recordingUrl"  style="width: 90px; left: 0px;  "><div><a href="javascript:;" ><i ></i></a> <a href="javascript:;"  style="display: none;"><i ></i></a></div></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="123" comp-id="4613" col-id="campaignName"  style="width: 209px; left: 90px;  "><span><span title="Click to Filter" >Window Installation Inbound</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="124" comp-id="4614" col-id="publisherName"  style="width: 209px; left: 299px;  "><span><span title="Click to Filter" >Aef media group</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="125" comp-id="4615" col-id="inboundPhoneNumber"  style="width: 139px; left: 508px;  "><span><span title="" > 12818548738</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="126" comp-id="4616" col-id="number"  style="width: 139px; left: 647px;  "><span><span title="Click to Filter" > 18333361696</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="127" comp-id="4617" col-id="timeToCallInSeconds"  style="width: 70px; left: 786px;  "><span><span title="Click to Filter" >00:00:00</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="128" comp-id="4618" col-id="isDuplicate"  style="width: 70px; left: 856px;  "><span><span title="Click to Filter" >Yes</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="129" comp-id="4619" col-id="endCallSource"  style="width: 56px; left: 926px;  "><span><span title="Caller" ></span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="130" comp-id="4620" col-id="timeToConnectInSeconds"  style="width: 70px; left: 982px;  "><span>00:00:03</span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="131" comp-id="4621" col-id="targetName"  style="width: 209px; left: 1052px;  "><span><span title="Click to Filter" >Window-2</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="132" comp-id="4622" col-id="conversionAmount"  style="width: 69px; left: 1261px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="133" comp-id="4623" col-id="payoutAmount"  style="width: 70px; left: 1330px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="134" comp-id="4624" col-id="callLengthInSeconds"  style="width: 70px; left: 1400px;  "><span><span title="Click to Filter" >00:00:37</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="135" comp-id="4625" col-id="action"  style="width: 80px; left: 1470px;  "><span><div>
              <a title="Block Number" >
                    <i ></i>
                  </a>

              <a title="Add Tag Annotation" >
                <i ></i>
              </a>

              <a title="Adjust Call Payments" >
                  <i ></i>
                </a>
            </div></span></div></div><div role="row" row-index="3" aria-rowindex="7" row-id="RGB98CC6997C0F7F7C1456B8E03D534E6434C4328A9V3ILT01" comp-id="4627"  aria-selected="false" style="height: 29.6px; transform: translateY(88.80000000000001px); " aria-label="Press SPACE to select this row."><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="122" comp-id="4628" col-id="recordingUrl"  style="width: 90px; left: 0px;  "><div><a href="javascript:;" ><i ></i></a> <a href="javascript:;"  style="display: none;"><i ></i></a></div></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="123" comp-id="4629" col-id="campaignName"  style="width: 209px; left: 90px;  "><span><span title="Click to Filter" >Window Installation Inbound</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="124" comp-id="4630" col-id="publisherName"  style="width: 209px; left: 299px;  "><span><span title="Click to Filter" >Aef media group</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="125" comp-id="4631" col-id="inboundPhoneNumber"  style="width: 139px; left: 508px;  "><span><span title="" > 19549456507</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="126" comp-id="4632" col-id="number"  style="width: 139px; left: 647px;  "><span><span title="Click to Filter" > 18333361696</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="127" comp-id="4633" col-id="timeToCallInSeconds"  style="width: 70px; left: 786px;  "><span><span title="Click to Filter" >00:00:00</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="128" comp-id="4634" col-id="isDuplicate"  style="width: 70px; left: 856px;  "><span><span title="Click to Filter" >No</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="129" comp-id="4635" col-id="endCallSource"  style="width: 56px; left: 926px;  "><span><span title="Caller" ></span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="130" comp-id="4636" col-id="timeToConnectInSeconds"  style="width: 70px; left: 982px;  "><span>00:00:03</span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="131" comp-id="4637" col-id="targetName"  style="width: 209px; left: 1052px;  "><span><span title="Click to Filter" >Window-2</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="132" comp-id="4638" col-id="conversionAmount"  style="width: 69px; left: 1261px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="133" comp-id="4639" col-id="payoutAmount"  style="width: 70px; left: 1330px;  "><span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="134" comp-id="4640" col-id="callLengthInSeconds"  style="width: 70px; left: 1400px;  "><span><span title="Click to Filter" >00:01:18</span></span></div><div tabindex="-1" unselectable="on" role="gridcell" aria-colindex="135" comp-id="4641" col-id="action"  style="width: 80px; left: 1470px;  "><span><div>
              <a title="Block Number" >
                    <i ></i>
                  </a>

              <a title="Add Tag Annotation" >
                <i ></i>
              </a>

              <a title="Adjust Call Payments" >
                  <i ></i>
                </a>
            </div></span></div></div></div>

I just want to iterate the divs and get the data out of it. I tried this code:

try:
    number = drive.find_element(By.CSS_SELECTOR, "div[col-id='inboundPhoneNumber']")
    print(number.text)
except:
    print(" NOTHING FOUND ")

But I failed to achieve my target. I was trying to get all the numbers from the divs. Is there any easier solution for me. I am so new in selenium.

CodePudding user response:

You did not post a minimal, reproducible example. However, you will need the following imports:

[..]
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]

Then (best practice) you need to wait out the elements, like so:

wait = WebDriverWait(driver, 25)
[..]
elements = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@col-id='inboundPhoneNumber']")))
for e in elements:
    print(e.text)

See Selenium documentation here.

  • Related