Home > Blockchain >  How does one find_element by text and then copy the next sibling element?
How does one find_element by text and then copy the next sibling element?

Time:09-29

I am trying to learn python selenium and I have been stuck for days trying to figure out the best way for me to extract only the date from this little chunk of code. My best guess was to

driver.find_element(By.XPATH text()contains 'registration date' blah

then somehow find the next element that comes directly after that, but every time I retrieve the element from the page it gives it to me in the form of:

element="37253116-f015-40a1-bdb1-135342333393"

but I haven't the slightest clue about what that even is - let alone what I could do with it.

Also, Pandas doesn't recognize this chunk of code as a table and ignores it when looking for tables, sadly. If anybody has any ideas it would be greatly appreciated.. Unless it's by making a loop in BeautifulSoup..

<dl >
    <dt>MASTER NAME</dt>
    <dd>Napoleon</dd>
    <dt>BUSINESS TYPE</dt>
    <dd>Oxygen farm</dd>
    <dt>FILE NUMBER</dt>
    <dd>94785394</dd>
    <dt>STATUS</dt>
    <dd>Active</dd>
    <dt>PURPOSE</dt>
    <dd> relaxation </dd>
    <dt>PLACE INCORPORATED</dt>
    <dd> the moon </dd>
    <dt>REGISTRATION DATE</dt>
    <dd>Dec 8, 1986</dd>
    <dt>MAILING ADDRESS</dt>
    <dd> fun land <br>
    THE MOON 
</dd>
              </dl>

CodePudding user response:

this xpath should work for you - it will return you the first sibling of the Registration date dt element:

//dl[@class = 'space']/dt[text() = 'REGISTRATION DATE']/following-sibling::dd[1]

and then you can call the method for fetching the text of this found element

CodePudding user response:

suppose you want the code similar as the following, locate one element by xpath, and then iterate the next sibling element

from clicknium import clicknium as cc

if not cc.chrome.extension.is_installed():
    cc.chrome.extension.install_or_update()
tab = cc.chrome.open("https://www.bing.com/search?q=clicknium")
elem = tab.find_element_by_xpath('//*[@id="b_results"]/li[2]')

while elem != None:
    print(elem.get_text())
    elem = elem.next_sibling
  • Related