Home > Net >  SELENIUM FIND CHILD ELEMENTS OF GOT PARENT ELEMENT
SELENIUM FIND CHILD ELEMENTS OF GOT PARENT ELEMENT

Time:10-06

First of all, respect and greetings to everyone.
I am building an automation application together with python and selenium. In general, I take elements that repeat in the DOM in a batch way with the find_elements method and save them in a variable. EX;

HTML ELEMENT THAT I CALLED GENERAL

<div >
<h3 >
    <div ><span ><a
            
            href="/zelihaozbey6/" role="link" tabindex="0">zelihaozbey6</a></span></div>
</h3>
<div ><span
        >Emeğine sağlık çok güzel olmuşlar maşallah</span></div>
<div >
    <div ><a
            
            href="/p/CKddDXShtW_/c/17935291282568260/" role="link" tabindex="0">
        <time  datetime="2021-07-08T20:33:35.000Z" title="Tem 8, 2021">64h</time>
    </a>
        <button >
            <div >Yanıtla</div>
        </button>
        <div ></div>
        <div >
            <div >
                <button  type="button">
                    <div >
                        <div  style="height: 24px; width: 24px;">
                            <svg aria-label="Yorum Seçenekleri"  color="#8e8e8e" fill="#8e8e8e"
                                 height="24" role="img" viewBox="0 0 24 24" width="24">
                                <circle cx="12" cy="12" r="1.5"></circle>
                                <circle cx="6" cy="12" r="1.5"></circle>
                                <circle cx="18" cy="12" r="1.5"></circle>
                            </svg>
                        </div>
                    </div>
                </button>
            </div>
        </div>
    </div>
</div>

consts.py

GENERAL_COMMENT_SECTION = "//ul//ul/div/li/div"
COMMENTS_BY_XPATH = "//ul//ul/div/li/div/div/div/div/span"
USERNAME_BY_XPATH = "//ul//ul/div/li/div/div/div/h3"
COMMENT_TIME = "//ul//ul/div/li/div/div/div/div/div/a/time"

EX;

comments = self.wait.until(
        EC.presence_of_all_elements_located((By.XPATH, GENERAL_COMMENT_SECTION)))

with open('src/comment.txt', 'a ', encoding="utf-8") as f:
     for comment in comments:
        username = comment.find_element(By.XPATH, USERNAME_BY_XPATH).text
        comment_text = comment.find_element(By.XPATH, COMMENTS_BY_XPATH).text
        comment_time = comment.find_element(By.XPATH, COMMENT_TIME).get_attribute("title")
        f.write(f"{username},{comment_text},{comment_time}\n")
     f.close()

OUTPUT comments.txt

armaganfadik,Tosbikk ya,Oca 2, 2022
armaganfadik,Tosbikk ya,Oca 2, 2022
armaganfadik,Tosbikk ya,Oca 2, 2022
armaganfadik,Tosbikk ya,Oca 2, 2022

Then I aim to reach each element in the loop and get some texts and title attributes inside the element. But only the information of the first element in the loop comes. How can I solve this

CodePudding user response:

since you are not referencing the child of the parent node, it will always return the first element.

Try out this one which reference the child of parent element.

GENERAL_COMMENT_SECTION = "//ul//ul/div/li/div"
COMMENTS_BY_XPATH = "./div/div/div/span"
USERNAME_BY_XPATH = "./div/div/h3"
COMMENT_TIME = "./div/div/div/div/a/time"
  • Related