xpath for svg ,cannot locate based on path attribute - python

I want to locate icon based on SVG. Xpath is not working. Only way to distinguish both icon is using this attribute , but that is not working.
<path d="M8 5v14l11-7z"
I have tried:
//*[name()='svg']/*[name()='path' and #d="M8 5v14l11-7z"]
No luck
<div class="page-details-box-links ng-scope" style="">
<button class="icon-style >
<div c style="">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="">
<path d="M8 5v14l11-7z" style=""></path>
<path d="M0 0h24v24H0z" fill="none" style=""></path>
</svg>
</div>
<div style="">Run job</div>
</button>
<button style="">
<div style="">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="">
<path d="M0 0h24v24H0z" fill="none" style=""></path>
<path d="M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07-.98l2.11-1.65c.19-.15.24-.42.12-.64l-2-3.46c-.12-.22-.39-.3-.61-.22l-2.49 1c-.52-.4-1.08-.73-1.69-.98l-.38-2.65C14.46 2.18 14.25 2 14 2h-4c-.25 0-.46.18-.49.42l-.38 2.65c-.61.25-1.17.59-1.69.98l-2.49-1c-.23-.09-.49 0-.61.22l-2 3.46c-.13.22-.07.49.12.64l2.11 1.65c-.04.32-.07.65-.07.98s.03.66.07.98l-2.11 1.65c-.19.15-.24.42-.12.64l2 3.46c.12.22.39.3.61.22l2.49-1c.52.4 1.08.73 1.69.98l.38 2.65c.03.24.24.42.49.42h4c.25 0 .46-.18.49-.42l.38-2.65c.61-.25 1.17-.59 1.69-.98l2.49 1c.23.09.49 0 .61-.22l2-3.46c.12-.22.07-.49-.12-.64l-2.11-1.65zM12 15.5c-1.93 0-3.5-1.57-3.5-3.5s1.57-3.5 3.5-3.5 3.5 1.57 3.5 3.5-1.57 3.5-3.5 3.5z" style=""></path>
</svg>
</div>
<div class="cv-material-button-tooltip" style="">Edit</div>
</button>
</div>

try with this xpath :
//button[contains(#class,'con-styl')]//*[name()='svg']/*[1]

To locate the icon based on SVG you can use the following XPath:
//div[#class='page-details-box-links ng-scope']/button[#class='icon-style']//*[name()='svg']//*[name()='path' and #d='M8 5v14l11-7z']
Note: The element is Angular element so you have to induce a waiter through WebDriverWait

Related

SELENIUM FIND CHILD ELEMENTS OF GOT PARENT ELEMENT

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
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"

I am trying to make a BOT in selenium, which will open google classroom and open the meeting, but

I am trying to make a BOT in selenium, which will open google classroom and open the meeting, but the problem is everything is working fine just Its not turning off the camera on google meet, I am new to selenium thing, So please help me out
My Code:
cameraOff = WebDriverWait(driver,30).until(
EC.presence_of_element_located((By.CLASS_NAME,"U26fgb"))
)
cameraOff.click()
and yeah I imported everything correctly so there's no importing problem, I just want to turn of camera.
Screenshot:
Inspect HTML Code:
Text Code of Inspect HTML:
<div jsshadow="" role="button" class="U26fgb JRY2Pb mUbCce kpROve uJNmj QmxbVb FTMc0c N2RpBe jY9Dbb M9Bg4d" jscontroller="lCGUBd" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd" jsname="BOHaEe" aria-disabled="false" tabindex="0" data-response-delay-ms="250" data-tooltip-position="top" data-tooltip-vertical-offset="12" aria-label="Turn on camera (ctrl + e)" data-tooltip="Turn on camera (ctrl + e)" data-is-muted="true"><div class="I5fjHe wb61gb"></div><div class="VTBa7b MbhUzd" jsname="ksKsZd" style="top: 26px; left: -59.5px; width: 54px; height: 54px;"></div><span jsslot=""><span class="DPvwYc JnDFsc dMzo5" aria-hidden="true"><div jscontroller="WaSQmd" jsaction="M8Qplc:BE5wSe;sO4MKe:Z1hKWc" aria-hidden="true" data-icon-type="1" class="IYwVEf nAZzG"><div class="oTVIqe BcUQQ" jsname="qmYRK" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="48" height="48" preserveAspectRatio="xMidYMid meet" style="width: 100%; height: 100%;"><defs><clipPath id="animationMask_ps9OVEcY4t"><rect width="48" height="48" x="0" y="0"></rect></clipPath><mask id="RJypApJvUC"><rect fill="#ffffff" width="48" height="48" x="-24" y="-24"></rect><path fill="#000000" clip-rule="nonzero" d=" M-7.77,-10.599 C-7.77,-10.599 -9.19,-9.189 -9.19,-9.189 C-9.19,-9.189 9.2,9.201 9.2,9.201 C9.2,9.201 10.61,7.791 10.61,7.791 C10.61,7.791 -7.77,-10.599 -7.77,-10.599" fill-opacity="1"></path></mask></defs><g clip-path="url(#animationMask_ps9OVEcY4t)"><g mask="url(#RJypApJvUC)" transform="matrix(1.35,0,0,1.35,24,24)" opacity="1" style="user-select: none;"><g opacity="1" transform="matrix(1,0,0,1,0,0)"><g opacity="1" transform="matrix(1,0,0,1,0,0)"><path class="fill bkbMM" fill="rgb(255,255,255)" fill-opacity="1" d="M0 0 M-9.19,-9.189 C-9.19,-9.189 -10.61,-7.779 -10.61,-7.779 C-10.61,-7.779 7.78,10.611 7.78,10.611 C7.78,10.611 9.19,9.201 9.19,9.201 C9.19,9.201 -9.19,-9.189 -9.19,-9.189z"></path></g><g opacity="1" transform="matrix(1,0,0,1,0,0)"><path class="fill bkbMM" fill="rgb(255,255,255)" fill-opacity="1" d="M0 0 M4,-2.31 C4,-2.31 4,6 4,6 C4,6 -8,6 -8,6 C-8,6 -8,-6 -8,-6 C-8,-6 4,-6 4,-6 C4,-6 4,-2.31 4,-2.31zM0 0 M6,-1.52 C6,-1.52 6,-6 6,-6 C6,-7.1 5.1,-8 4,-8 C4,-8 -8,-8 -8,-8 C-9.1,-8 -10,-7.1 -10,-6 C-10,-6 -10,6 -10,6 C-10,7.1 -9.1,8 -8,8 C-8,8 4,8 4,8 C5.1,8 6,7.1 6,6 C6,6 6,1.52 6,1.52 C6,1.52 10,5.5 10,5.5 C10,5.5 10,-5.5 10,-5.5 C10,-5.5 6,-1.52 6,-1.52z"></path></g></g></g></g></svg></div><span class="DPvwYc cR8Azd bkl1qf" aria-hidden="true"><svg focusable="false" width="24" height="24" viewBox="0 0 24 24" class="Hdh4hc cIGbvc NMm5M"><path d="M18 10.48V6c0-1.1-.9-2-2-2H6.83l2 2H16v7.17l2 2v-1.65l4 3.98v-11l-4 3.98zM16 16L6 6 4 4 2.81 2.81 1.39 4.22l.85.85C2.09 5.35 2 5.66 2 6v12c0 1.1.9 2 2 2h12c.34 0 .65-.09.93-.24l2.85 2.85 1.41-1.41L18 18l-2-2zM4 18V6.83L15.17 18H4z"></path></svg></span><span class="DPvwYc zYrqtc thO1y" aria-hidden="true"><svg focusable="false" width="24" height="24" viewBox="0 0 24 24" class="Hdh4hc cIGbvc NMm5M"><path d="M18 10.48V6c0-1.1-.9-2-2-2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2v-4.48l4 3.98v-11l-4 3.98zm-2-.79V18H4V6h12v3.69z"></path></svg></span></div></span></span></div>
Actually I don't know much about css selector, So please help me out
Every answer will be appreciated, Thanks in advance
Use ActionChains
for example:
ActionChains(driver).key_down(Keys.CONTROL).send_keys('e').key_up(Keys.CONTROL).perform()
This will work as pressing Ctrl+e and it will turn off the camera
reference: https://selenium-python.readthedocs.io/api.html#selenium.webdriver.common.action_chains.ActionChains.key_down

Web parsing using beautiful soup

I need to parse data from a website: https://finance.yahoo.com/quote/MSFT/community.
I am trying to scrape the time of the post. For example "21 hours ago".
HTML code of the site. I am trying to extract time from
<span class = "F....>21 hours ago<...
<li class="comment Pend(2px) Mt(5px) Mb(11px) P(12px) " data-reactid="24">
<div class="Pos(r) Pstart(52px) " data-reactid="25">
<div class="Fz(12px) Mend(20px) Mb(5px)" data-reactid="26">
<div class="avatar D(ib) Bdrs(46.5%) Pos(a) Start(0) Cur(p)" data-reactid="27">...</div>
<button aria-label="See reaction history for 💰Chef💰" class="D(ib) Fw(b) P(0) Bd(0) M(0) Mend(10px)
Fz(16px) Ta(start) C($c-fuji-blue-1-a)" data-reactid="31">💰Chef💰</button>
<span class="Fz(12px) C(#828c93)"><span>21 hours ago</span></span>
<div class="Wow(bw)" data-reactid="33">...</div>
<div class="Py(4px)" data-reactid="39">...</div>
<div>...</div>
<div class="Pos(r) Pt(5px)" data-reactid="45">...</div>
</div>
</li>
The issue is, I am not able to find the time after reading the data using beautifulsoup. Here is the code which I have written.
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
content = soup.find('ul' , class_= "comments-list List(n) Ovs(touch) Pos(r) Mt(10px) Mstart(-12px) Pt(5px)")
li = content.find('li' , class_ = "comment Pend(2px) Mt(5px) Mb(11px) P(12px)")
print(li.prettify())
Output
li = content.find('li' , class_ = "comment Pend(2px) Mt(5px) Mb(11px) P(12px)")
print(li.prettify())
<li class="comment Pend(2px) Mt(5px) Mb(11px) P(12px)" data-reactid="24">
<div class="Pos(r) Pstart(52px)" data-reactid="25">
<div class="Fz(12px) Mend(20px) Mb(5px)" data-reactid="26">
<div class="avatar D(ib) Bdrs(46.5%) Pos(a) Start(0) Cur(p)" data-reactid="27">
<div class="Pos(r)" data-reactid="28">
<div class="avatar-text Ta(c) Bdrs(48%)" data-reactid="29" style="background-color:#ff333a;color:#fff;font-size:24px;line-height:40px;width:40px;height:40px;" title="See reaction history for 💰Chef💰">
�
</div>
<img alt="💰Chef💰" class="avatar-img Bdrs(48%) Pos(a) StretchedBox Bgc(#400090.03)" data-reactid="30" height="40" src="https://s.yimg.com/it/api/res/1.2/H4mqpmacU.CnalUl7leuZA--~A/YXBwaWQ9eW5ld3M7dz04MDtoPTgwO3E9ODA-/https://s.yimg.com/gq/1735/40147141066_fc6972_o.jpg" title="See reaction history for 💰Chef💰" width="40"/>
</div>
</div>
<button aria-label="See reaction history for 💰Chef💰" class="D(ib) Fw(b) P(0) Bd(0) M(0) Mend(10px) Fz(16px) Ta(start) C($c-fuji-blue-1-a)" data-reactid="31">
💰Chef💰
</button>
<!-- react-empty: 32 -->
</div>
<div class="Wow(bw)" data-reactid="33">
<div class="C($c-fuji-grey-l) Mb(2px) Fz(14px) Lh(20px) Pend(8px)" data-reactid="34">
<!-- react-text: 35 -->
I managed to pick up an Xbox series X and I have to say the tech is very impressive (bought it not knowing). This only makes up around 10% of Microsoft’s business and slated to out perform PS5. I personally believe this stock is still undervalued. Longs stay strong!
<!-- /react-text -->
</div>
<div class="Pos(r) D(ib) Lh(12px)" data-reactid="36">
<video autoplay="" class="W(a) Mah(320px) My(3px) H(a) Maw(100%)" data-reactid="37" height="236" loop="" muted="" playsinline="" poster="https://media.tenor.com/images/e239c83d92cd0e896dc8b9ac05be0bf4/tenor.png" width="258">
<source data-reactid="38" src="https://media.tenor.com/videos/1eff610b01f69f793fe1f267c88bbbd4/mp4" type="video/mp4"/>
</video>
</div>
</div>
<div class="Py(4px)" data-reactid="39">
<div class="D(ib) Pos(r)" data-reactid="40">
<div class="Fz(12px) Px(8px) Mend(4px) Va(m) Bdrs(3px) C($c-fuji-grey-g) Bgc($c-fuji-grey-b) Cur(d) Py(3px)" data-reactid="41">
<div class="D(ib) Mend(6px) Cur(d) Cur(p)" data-icon="traffic" data-reactid="42" style="vertical-align:middle;fill:#333;stroke:#333;stroke-width:0;width:18px;height:18px;">
</div>
<!-- react-text: 43 -->
Bullish
<!-- /react-text -->
</div>
</div>
</div>
<!-- react-empty: 44 -->
<div class="Pos(r) Pt(5px)" data-reactid="45">
<div class="D(ib)" data-reactid="46">
<button class="reply-button O(n):h O(n):a P(0) Bd(n) Cur(p) Fz(12px) Fw(n) C(#828c93) D(ib) Mend(20px)" data-reactid="47">
<svg class="Mend(6px) Cur(p)" data-icon="reply" data-reactid="48" height="15" style="fill:#828c93;stroke:#828c93;stroke-width:0;vertical-align:bottom;" viewbox="0 0 25 20" width="15">
<path d="M12.8 5.8V2.4c0-.4-.2-.7-.6-.8-.3-.2-.8-.1-1.1.1L.9 8.8c-.2.2-.4.4-.4.7s.2.6.4.8l10.2 7.1c.3.2.7.3 1.1.1.3-.2.6-.5.6-.8v-3.3h1.3c3 0 6.1.6 8.5 5.2.2.3.5.5.9.5h.3c.5-.1.8-.6.7-1C23 7.3 18.5 5.9 12.8 5.8zm-.1 5.6h-1c-.6 0-1 .4-1 .9v2.4L3.2 9.5l7.5-5.2v2.4c0 .5.5.9 1 .9 4.2 0 7.9 0 9.8 6.3-2.9-2.6-6.3-2.6-8.8-2.5z" data-reactid="49">
</path>
</svg>
<!-- react-text: 50 -->
Reply
<!-- /react-text -->
</button>
</div>
<div class="D(ib) Pos(r)" data-reactid="51">
<button aria-label="8 Thumbs Up" class="O(n):h O(n):a Bgc(t) Bdc(t) M(0) P(0) Bd(n) Mend(20px)" data-reactid="52">
<svg class="Cur(p)" data-icon="thumbsup-outline" data-reactid="53" height="12" style="vertical-align:middle;fill:#828c93;stroke:#828c93;stroke-width:0;" viewbox="0 0 24 24" width="12">
<path d="M2.4 21.7V11.6h2.1c1.1 0 1.7-.7 2.1-1.1.7-.7 1.5-1.4 2.2-2.1.3-.2.5-.4.7-.6 1.6-1.8 2.3-3.4 2.2-5.5 0 0 .1-.1.3 0 .5.1 1 .6 1.1 1.6.3 2.4-.2 4.3-.7 4.7-.9.7-.4 2 .8 2h7.1c.7 0 1.3.4 1.3.8 0 .3-.5.8-.7.8-1.6 0-1.6 2.1-.1 2.2 0 .1.1.2.1.4 0 .1 0 .2-.3.3-.3-.1-.5-.1-.7-.1-1.3 0-1.7 1.7-.5 2.2 0 0 .2.1.3.2.2.2.3.3.3.6 0 .1-.1.1-.3.3-.2.1-.5.2-.7.2-1.3.2-1.4 1.9-.1 2.2l.1.1s.2.2.2.5c0 .4-.2.6-.6.6l-16.2-.2zM20.3 8.3h-5.1c.4-1.3.6-2.9.3-4.7-.2-2-1.5-3.3-3.1-3.6-1.7-.3-3.2.7-3.1 2.4.1 1.5-.4 2.6-1.7 4l-.5.5c-.6.7-1.3 1.4-2 2-.3.2-.5.4-.7.5H1.2c-.7 0-1.2.5-1.2 1.1v12.3c0 .7.6 1.2 1.2 1.2h17.3c1.6 0 3-1 3-2.8 0-.5-.1-.9-.3-1.2.7-.5 1.1-1.1 1.1-2 0-.5-.1-.9-.3-1.3.7-.4 1.2-1.1 1.2-2 0-.4-.1-.9-.3-1.3.6-.5 1-1.3 1-2.1.1-1.8-1.7-3-3.6-3z" data-reactid="54">
</path>
</svg>
<span class="D(ib) Va(m) Fz(12px) Mstart(6px) C(#828c93)" data-reactid="55">
8
</span>
</button>
<button aria-label="0 Thumbs Down" class="O(n):h O(n):a Bgc(t) Bdc(t) M(0) P(0) Bd(n)" data-reactid="56">
<svg class="Cur(p)" data-icon="thumbsdown-outline" data-reactid="57" height="12" style="vertical-align:middle;fill:#828c93;stroke:#828c93;stroke-width:0;" viewbox="0 0 24 24" width="12">
<path d="M21.6 2.3v10.1h-2.1c-1.1 0-1.7.7-2.1 1.1-.7.7-1.5 1.4-2.2 2.1-.2.2-.4.4-.5.6-1.6 1.8-2.3 3.4-2.2 5.5 0 0-.1.1-.3 0-.5-.1-1-.6-1.1-1.6-.3-2.4.2-4.3.7-4.7.9-.7.4-2-.8-2H3.9c-.7 0-1.3-.4-1.3-.8-.2-.2.4-.6.5-.6 1.6 0 1.6-2.1.1-2.2 0-.1-.1-.2-.1-.4 0-.1 0-.2.3-.3s.5-.1.7-.1c1.3 0 1.7-1.7.5-2.2 0 0-.2-.1-.3-.2-.2-.2-.3-.3-.3-.6 0 0 .1-.1.3-.2.3-.1.6-.2.7-.2 1.3-.2 1.4-1.9.1-2.2L5 3.3s-.1-.2-.1-.5c0-.4.2-.6.6-.6l16.1.1zM0 12.7c0 1.8 1.8 3 3.7 3h5.1c-.4 1.3-.6 2.9-.3 4.7.3 1.9 1.5 3.3 3.1 3.5 1.7.3 3.2-.7 3.1-2.4-.1-1.5.4-2.6 1.7-4 .2-.2.3-.4.5-.5.7-.7 1.4-1.3 2.1-1.9.3-.2.5-.4.6-.5h3.2c.7 0 1.2-.5 1.2-1.1V1.2c0-.7-.6-1.2-1.2-1.2H5.5c-1.6 0-3 1-3 2.8 0 .5.1.9.3 1.2-.8.5-1.2 1.2-1.2 2 0 .5.1.9.3 1.3-.7.4-1.2 1.1-1.2 2 0 .4.1.9.3 1.3-.6.6-1 1.3-1 2.1" data-reactid="58">
</path>
</svg>
</button>
</div>
</div>
</div>
</li>
If I print this line of code the output says "None".
date = content.find('span' , class_="Fz(12px) C(#828c93)")
print(date)
Output
None
Can someone explain me why this is happening or help with the code which reads the time of the post (21 hours ago)?
Thank you .
You get output as "None" because BeautifulSoup gets the HTML Source from the initial Request and it doesn't have the Posted time values. Those values are rendered after the initial request. To capture those kinds of values you can use a library like "Selenium". When you use selenium, you can wait till the page loads and then pass that HTML Source into BeautifulSoup and try out. Below is a sample code created to get a basic understanding:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://finance.yahoo.com/quote/MSFT/community')
html = driver.page_source
try:
myElem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'comment Pend(2px) Mt(5px) Mb(11px) P(12px)')))
soup = BeautifulSoup(html, 'html.parser')
# Rest of the code
except TimeoutException:
print("Load failed")
import requests
from bs4 import BeautifulSoup
response = requests.get("https://finance.yahoo.com/quote/MSFT/community")
soup = BeautifulSoup(response.content, 'html.parser')
print(soup)

Python selenium find checkbox

I'm trying to find check box and click in it. I've tried using:
driver.find_element_by_xpath(("//*[contains(text(), '2+ interchanges')]")).click()
or
driver.find_element_by_class_name('BpkCheckbox_bpk-checkbox__label__1vrLS BpkCheckbox_bpk-checkbox__label--small__16XBT')
or
driver.find_element_by_name('2+ interchanges').click()
but no luck, always get error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:
Could you please give me some advice how i do should find this checkbox?
Thanks!
Best
The code looks like:
<div class="Filter_verticallySpaced__1Sm6F Filter_horizontallySpaced__RwfGp FiltersCheckboxes_colourOnHover__erAw_">
<label class="BpkCheckbox_bpk-checkbox__2yTOQ FiltersCheckboxes_checkboxLine__7IQLY">
<input aria-invalid="false" aria-label="2+ interchanges" checked="" class="BpkCheckbox_bpk-checkbox__input__2qMb7" name="twoPlusStops" type="checkbox"/>
<svg class="BpkCheckbox_bpk-checkbox__icon__1KuPa" height="18" style="width: 0.75rem; height: 0.75rem;" viewbox="0 0 24 24" width="18" xmlns="http://www.w3.org/2000/svg">
<path d="M9.4 20c-.5 0-.9-.2-1.3-.5l-5.8-5.1c-.4-.4-.5-1-.1-1.4l1.3-1.5c.4-.4 1-.5 1.4-.1l4 3.5c.2.1.4.1.6 0l9.2-10.5c.4-.4 1-.5 1.4-.1l1.5 1.3c.4.4.5 1 .1 1.4L10.9 19.3c-.4.5-.9.7-1.5.7z">
</path>
</svg>
<span aria-hidden="true" class="BpkCheckbox_bpk-checkbox__label__1vrLS BpkCheckbox_bpk-checkbox__label--small__16XBT">
2+ interchanges
</span>
</label>
<span class="BpkText_bpk-text__2NHsO BpkText_bpk-text--sm__345aT FiltersCheckboxes_subText__3aG9- FiltersCheckboxes_block__1ZTpQ FiltersCheckboxes_checkboxIndent__2c49O">
2 735 $
</span>
</div>
<div class="Filter_verticallySpaced__1Sm6F Filter_horizontallySpaced__RwfGp FiltersCheckboxes_colourOnHover__erAw_">
<label class="BpkCheckbox_bpk-checkbox__2yTOQ FiltersCheckboxes_checkboxLine__7IQLY">
<input aria-invalid="false" aria-label="1 interchange" checked="" class="BpkCheckbox_bpk-checkbox__input__2qMb7" name="oneStop" type="checkbox"/>
<svg class="BpkCheckbox_bpk-checkbox__icon__1KuPa" height="18" style="width: 0.75rem; height: 0.75rem;" viewbox="0 0 24 24" width="18" xmlns="http://www.w3.org/2000/svg">
<path d="M9.4 20c-.5 0-.9-.2-1.3-.5l-5.8-5.1c-.4-.4-.5-1-.1-1.4l1.3-1.5c.4-.4 1-.5 1.4-.1l4 3.5c.2.1.4.1.6 0l9.2-10.5c.4-.4 1-.5 1.4-.1l1.5 1.3c.4.4.5 1 .1 1.4L10.9 19.3c-.4.5-.9.7-1.5.7z">
</path>
</svg>
<span aria-hidden="true" class="BpkCheckbox_bpk-checkbox__label__1vrLS BpkCheckbox_bpk-checkbox__label--small__16XBT">
1 interchange
</span>
</label>
<span class="BpkText_bpk-text__2NHsO BpkText_bpk-text--sm__345aT FiltersCheckboxes_subText__3aG9- FiltersCheckboxes_block__1ZTpQ FiltersCheckboxes_checkboxIndent__2c49O">
2 787 $
</span>
</div>
With this
driver.find_element_by_class_name('BpkCheckbox_bpk-checkbox__label__1vrLS BpkCheckbox_bpk-checkbox__label--small__16XBT')
you picked two class names as one (between every class name is a space).
Try using this:
driver.find_element_by_class_name('BpkCheckbox_bpk-checkbox__label__1vrLS')
By the way- this
driver.find_element_by_name('2+ interchanges').click()
is searching for the name in an element (eg ), not the value within. (Your tag has no name attribute)
All right i found it :)
Solution, i just added wait for 10 secs before click, and changed xpath.
time.sleep(10)
stops_chkbox = driver.find_element_by_xpath('//*[#id="stops_content"]/div/div/div[3]/label').click()

Python Selenium - Can't Click on Button

All I am trying to do is select the drop down & then select "Export Excel Spread Sheet".
Example of Drop Down
Code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url2 =["https://example.com/reports"]
driver = webdriver.Chrome()
driver.implicitly_wait(15)
driver.get("https://example.com")
for u in url2:
driver.implicitly_wait(15)
driver.get(u)
I have tried so many different Xpaths & ID's
#driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]").click()
#driver.find_element_by_xpath("//li[contains(text(),'Export Excel Spread Sheet')]").click()
#act.click().perform()
#act.click(driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]")).perform()
#act.move_to_element(driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]")).perform()
#WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID, "Header-Dropdown-Menu")).click())
#driver.find_element_by_class_name("//div[contains(#class,'dropdown__content header-export-menu--content')]").click()
#driver.find_element_by_xpath('//div[#class="dropdown header-export-menu" and #class="dropdown dropdown--active header-export-menu"]')
#driver.quit()
HTML Code
Click Me For HTML Example1
<!-- Under React Empty: 32 -->
<div class = "dropdown header-export-menu">
Take a look at the name in Example 1 vs Example 2
Click Me for HTML Example2
You'll notice the HTML Code has change to
<!-- Under React Empty: 32 -->
<div class = "dropdown dropdown--active header-export-menu">
Which I think is part of the problem I am having. Pretty Stuck.
I have also tried to use ChroPath & XPath Helper to try and resolve the issue but no luck.
Thank you in Advance !
Update:
The Comments have asked for further detail of the HTML code & I have gathered the following block.
<div class="header-container">
<!-- react-empty: 429 -->
<div class="header-event-info" id="header-event-info">
<div class="">
<div>
<div class="single-event-info">
<div class="event-data">
<p class="data-dd">25</p>
<p class="data-mmyy">Jun 2017</p>
</div>
<div class="event-detail">
<p class="event-name">"A name of a musical"</p>
<p class="event-more-details">
<!-- react-text: 437 -->
"Tuesday, 7:00 pm, Some Theatre"
<!-- /react-text -->
<a class="popup" data-content="" data-icon="" data-position="bottom" data-width="350" data-height="auto" data-trigger="click" data-scrollable="false">
<span class="popup-icon">
<svg width="19px" height="19px" viewBox="0 0 19 19" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<desc>Created with sketchtool.</desc>
<defs></defs>
<g id="Totals-For-Today" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g class="svg-icon-path" id="01-Event-Audit-Narrow---No-TFT" transform="translate(-983.000000, -121.000000)" stroke="#919598">
<g id="Group" transform="translate(27.500000, 15.560000)">
<g id="iButton" transform="translate(956.000000, 106.000000)">
<path d="M9,17.4399996 C13.6944204,17.4399996 17.5,13.63442 17.5,8.93999958 C17.5,4.24557921 13.6944204,0.43999958 9,0.43999958 C4.30557963,0.43999958 0.5,4.24557921 0.5,8.93999958 C0.5,13.63442 4.30557963,17.4399996 9,17.4399996 Z" id="outline">
</path>
<path class="svg-icon-text" d="M10.4765625,13.3169527 L7.68164062,13.3169527 L7.68164062,12.930234 C7.77148482,12.9224214 7.86425733,12.914609 7.95996094,12.9067965 C8.05566454,12.8989839 8.13867152,12.8833591 8.20898438,12.8599215 C8.31835992,12.824765 8.3994138,12.7632422 8.45214844,12.6753511 C8.50488308,12.5874601 8.53125,12.4732034 8.53125,12.3325777 L8.53125,8.76421833 C8.53125,8.63921771 8.50292997,8.52496104 8.44628906,8.42144489 C8.38964815,8.31792875 8.31054738,8.23101556 8.20898438,8.16070271 C8.13476525,8.11382747 8.02734445,8.07378881 7.88671875,8.04058552 C7.74609305,8.00738223 7.61718809,7.98687462 7.5,7.97906208 L7.5,7.59820271 L9.5390625,7.46929646 L9.62109375,7.55132771 L9.62109375,12.2622652 C9.62109375,12.3989846 9.64746067,12.5122648 9.70019531,12.602109 C9.75292995,12.6919532 9.83593693,12.7583587 9.94921875,12.8013277 C10.0351567,12.8364841 10.1191402,12.8648042 10.2011719,12.8862886 C10.2832035,12.9077731 10.3749995,12.9224214 10.4765625,12.930234 L10.4765625,13.3169527 Z M9.73828125,5.18999958 C9.73828125,5.41265694 9.66503979,5.60699094 9.51855469,5.77300739 C9.37206958,5.93902385 9.19140732,6.02203083 8.9765625,6.02203083 C8.77734275,6.02203083 8.60449292,5.94293006 8.45800781,5.78472614 C8.31152271,5.62652223 8.23828125,5.44585997 8.23828125,5.24273396 C8.23828125,5.02788913 8.31152271,4.84039101 8.45800781,4.68023396 C8.60449292,4.5200769 8.77734275,4.43999958 8.9765625,4.43999958 C9.19921986,4.43999958 9.38183522,4.51519414 9.52441406,4.66558552 C9.6669929,4.81597689 9.73828125,4.99077983 9.73828125,5.18999958 L9.73828125,5.18999958 Z" id="i-2-copy-2" stroke-width="0.25" fill="#919598"></path>
</g>
</g>
</g>
</g>
</svg>
</span>
</a>
</p>
</div>
</div>
<div class="header-export">
<!-- react-empty: 32 -->
<div class="dropdown dropdown--active header-export-menu">
<a class="dropdown__trigger header-export-menu--toggle-btn">
<svg width="9px" height="5px" viewBox="0 0 9 5" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-109.000000, -97.000000)" fill="#FFFFFF">
<g id="Header-Dropdown-Menu" transform="translate(96.000000, 82.000000)">
<path d="M18.1734867,19.6470682 C17.8014721,20.0543213 17.1922167,20.0476427 16.8263028,19.6470682 L13.2549246,15.7373969 C12.8829101,15.3301438 13.0295754,15 13.5787039,15 L21.4210856,15 C21.9719185,15 22.1107787,15.3368224 21.7448649,15.7373969 L18.1734867,19.6470682 Z" id="options-dropdown-menu-arrow"></path>
</g>
</g>
</g>
</svg>
</a>
<div class="dropdown__content header-export-menu--content">
<ul class="export-menu">
<li class="export-menu-item ">
<svg width="17px" height="14px" viewBox="0 0 17 15" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-522.000000, -180.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Download-Icon-Copy-2" transform="translate(43.500000, 172.000000) rotate(-180.000000) translate(-43.500000, -172.000000) translate(35.000000, 164.000000)">
<path d="M5.36902902,13.624518 L5.36902902,6.56257607 L12.430971,6.56257607" id="Rectangle-242-Copy-5" transform="translate(8.900000, 10.093547) rotate(-315.000000) translate(-8.900000, -10.093547) "></path>
<path d="M8.9,6.99999999 L8.9,12.9999999" id="Line-Copy-10" stroke-linecap="square"></path>
<path d="M16.9,0.0208873076 L16.9,4.02297419 C16.9,5.12639113 16.0054862,6.02088731 14.9059397,6.02088731 L2.89406028,6.02088731 C1.7927712,6.02088731 0.9,5.12262668 0.9,4.02297419 L0.9,0.0208873076" id="Rectangle-243-Copy-4" transform="translate(8.900000, 3.020887) rotate(-180.000000) translate(-8.900000, -3.020887) "></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 55 -->
"Export PDF"
<!-- /react-text -->
</li>
<li class="export-menu-item">
<svg width="17px" height="14px" viewBox="0 0 17 15" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs><g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-522.000000, -180.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Download-Icon-Copy-2" transform="translate(43.500000, 172.000000) rotate(-180.000000) translate(-43.500000, -172.000000) translate(35.000000, 164.000000)">
<path d="M5.36902902,13.624518 L5.36902902,6.56257607 L12.430971,6.56257607" id="Rectangle-242-Copy-5" transform="translate(8.900000, 10.093547) rotate(-315.000000) translate(-8.900000, -10.093547) "></path>
<path d="M8.9,6.99999999 L8.9,12.9999999" id="Line-Copy-10" stroke-linecap="square">
</path>
<path d="M16.9,0.0208873076 L16.9,4.02297419 C16.9,5.12639113 16.0054862,6.02088731 14.9059397,6.02088731 L2.89406028,6.02088731 C1.7927712,6.02088731 0.9,5.12262668 0.9,4.02297419 L0.9,0.0208873076" id="Rectangle-243-Copy-4" transform="translate(8.900000, 3.020887) rotate(-180.000000) translate(-8.900000, -3.020887)
"></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 79 -->
"Export Excel Spread Sheet"
<!-- /react-text -->
</li>
<li class="export-menu-item ">
<svg width="16px" height="12px" viewBox="0 0 16 13" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-554.000000, -182.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Email-Icon-Copy" transform="translate(67.000000, 168.000000)">
<rect id="Rectangle-40" x="0" y="0.669998169" width="16" height="11" rx="2"></rect>
<path d="M1.55761719,3.08300781 L8.07275391,7.10009766 L14.8974609,3.14355469" id="Path-41"></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 90 -->
"Email/Schedule Report"
<!-- /react-text -->
</li>
</ul>
</div>
</div>
Update 2 (Solution)
Here's where I went wrong.
I didn't provide enough of the HTML code.
Just a few lines above there was an "iframe" which was not allowing me to enter the block of code.
After switching into the iframe, I was able to click into the button and complete the following task of exporting the excel report.
example of code (Generalized to your future endeavors)
#Finding the Frame
iframes = driver.find_element_by_id("IDofFrame")
#Switching to that frame
driver.switch_to.frame(iframes)
#Finding the dropdown button element
driver.find_element_by_xpath("XPathOfButton").click()
#delay on the export click
time.sleep(3)
#Export click
driver.find_element_by_xpath("XPathOfButtonToExport").click()
#If you need to switch out of the frame to go back to the original HTML block
driver.switch_to.default_content()
CHECK YOUR HTML CODE FOR FRAMES !!!!
good video to reference.
https://www.youtube.com/watch?v=NhRx99uFUNk
Actually your every attempt is incorrect.
driver.find_element_by_xpath("//li[contains(text(),'Export Excel
Spread Sheet')]").click()
Here you are using contains(text()) Which is incorrect actually if you pass the node set selected by text() to contains(), as you did, then it is converted to a string, by taking the string value of the first node in the node set while in your HTML I can see <svg> is the first inner node of the <li> element, I would suggest trying with dot . which will take all string value inside a node with explicit wait :
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//li[contains(.,'Export Excel Spread Sheet')]")))
element.click()
Hope it helps

Categories