Issue
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 class="_a9zr">
<h3 class="_a9zc">
<div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abbh _abcm"><span class="_aap6 _aap7 _aap8"><a
class="x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x9f619 x1ypdohk xt0psk2 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz _acan _acao _acat _acaw _a6hd"
href="/zelihaozbey6/" role="link" tabindex="0">zelihaozbey6</a></span></div>
</h3>
<div class="_a9zs"><span
class="_aacl _aaco _aacu _aacx _aad7 _aade">Emeğine sağlık çok güzel olmuşlar maşallah</span></div>
<div class="_ab8w _ab94 _ab99 _ab9f _ab9m _ab9p _abaj _abb- _abcm">
<div class="_aacl _aacn _aacu _aacy _aad6"><a
class="x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x9f619 x1ypdohk xt0psk2 xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1a2a7pz _a9zg _a6hd"
href="/p/CKddDXShtW_/c/17935291282568260/" role="link" tabindex="0">
<time class="_a9ze _a9zf" datetime="2021-07-08T20:33:35.000Z" title="Tem 8, 2021">64h</time>
</a>
<button class="_a9ze">
<div class="_aacl _aacn _aacw _aacy _aad6 _aade">Yanıtla</div>
</button>
<div class=" _ab8y _ab94 _ab99 _ab9f _ab9m _ab9p _abbi _abcm"></div>
<div class="_a9ze">
<div class=" _a9zi">
<button class="_abl-" type="button">
<div class="_abm0">
<div class="_ab8w _ab94 _ab97 _ab9h _ab9m _ab9p _abcm" style="height: 24px; width: 24px;">
<svg aria-label="Yorum Seçenekleri" class="_ab6-" 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
Solution
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"
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.