How to identify button class element? - python

I tried the following to identify elements but I am getting "No element found" message when I run my scripts.
Method1 tried:
self.driver.find_element_by_xpath("//button[text()='Adopt and Initial']").click()
Method2 tried:
self.driver.find_element_by_css_selector(".btn-primary.btn.left.item-alt").click()
HTML of the button:
Updated Html code for the element. This is for docusign.
<div class="dialog is-signature-mode" tabindex="0">
<header class="dialog-header">
<h1 class="dialog-title">
<span class="item-alt" data-group="tagType" data-group-item="signature">Adopt Your Signature</span>
<span class="item-alt" data-group="tagType" data-group-item="initials" data-selected="">Adopt Your Initials</span>
</h1>
<nav class="icons">
<a class="close" data-action="cancelAdoptSignature">
<i class="icon-close"></i>
</a>
</nav>
</header>
<section class="dialog-body">
<article id="adopt">
<header class="ds-title p">
Confirm your name, initials, and signature.
</header>
<div class="full-name">
<div class="wrapper">
<label for="full-name">Full Name</label> <span class="error hidden">Name required</span>
<br>
<div class="text-input-wrapper">
<input id="full-name" disabled="" value="QAAuto 01Dec2014_10.41.03" name="fullname" type="text" class="required text-input" maxlength="50">
</div>
</div>
</div>
<div class="initials">
<div class="wrapper">
<label for="initials">Initials</label> <span class="error hidden">Initials required</span>
<br>
<div class="text-input-wrapper">
<input id="initials" disabled="" value="Q0" name="initials" type="text" class="required text-input" maxlength="50">
</div>
</div>
</div>
<div class="clear-float"></div>
</article>
<header class="tab-nav">
<ul>
<li>Select Style</li>
<li>Draw</li>
</ul>
</header>
<article id="select-style" class="tab-panel panel-select-style selected">
<h4 class="normal">Preview <span class="error"></span></h4>
<div class="signature-preview">
<div class="signature"><img alt="" src="https://demo.docusign.net/Signing/image.aspx?ti=56b2faad38e7427a99defd1dfaa258ce&insession=1&i=asig150&force=154&s=QAAuto+01Dec2014_10.41.03&f=7_DocuSign&nochrome=0" height="75px" class="signature-img left">
<img alt="" src="https://demo.docusign.net/Signing/image.aspx?ti=56b2faad38e7427a99defd1dfaa258ce&insession=1&i=ainit150&force=155&s=Q0&n=QAAuto+01Dec2014_10.41.03&f=7_DocuSign&nochrome=0" height="75px" class="initials-img right">
<div class="clear-float"></div></div>
<a class="change-style">
Change Style
</a>
<div class="clear-float"></div>
</div>
</article>
<article id="draw" class="tab-panel panel-draw">
<h4 class="normal">
<span class="item-alt-inline" data-group="tagType" data-group-item="signature">Draw your signature</span>
<span class="item-alt-inline" data-group="tagType" data-group-item="initials" data-selected="">Draw your initials</span>
<span class="error"></span>
</h4>
<a class="clear" data-ds="clear">Clear</a>
<div class="signature-draw signature">
<div class="canvas-wrapper"><canvas class="canvas" width="0" height="0"></canvas><canvas class="canvas" width="0" height="0"></canvas></div></div>
</article>
<p class="legalese">By clicking Adopt and Sign, I agree that the signature and initials will be the electronic representation of my signature and initials for all purposes when I (or my agent) use them on documents, including legally binding contracts - just the same as a pen-and-paper signature or initial.</p>
<hr>
<button class="btn-primary btn left item-alt" data-group="tagType" data-group-item="signature" type="button" data-ds="submit" value="initials">Adopt and Sign</button>
<button class="btn-primary btn left item-alt" data-group="tagType" data-group-item="initials" type="button" data-ds="submit" value="initials" data-selected="">Adopt and Initial</button>
<button class="close left btn btn-default" type="button" data-action="cancelAdoptSignature">Cancel</button>
<div class="clear-float"></div>
<div class="styles"></div></section>
</div>

Please try below xpath and let me know what happens
assertTrue(driver.getPageSource().contains("Adopt and Initial"));
self.driver.find_element_by_xpath("//button[conatins(text(),'Adopt and Initial')]".click();
Now Make sure you use assertion before the command because if the assertion fails then the element is not present in the current frame. so we will have to switch the correct frame before we perform the action.
Let me know if you try this.

Related

python selenium, cant retrieve text of xpath

Im struggling with scraping a few pages ... it happens when the structure of the page implies a lot of nested divs...
Here is the code page:
<div>
<section class="ui-accordion-header ui-state-default ui-corner-all ui-accordion-icons" role="tab" id="ui-id-1" aria-controls="ui-id-2" aria-selected="false" aria-expanded="false" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
<div class="detail-avocat">
<div class="nom-avocat">Me <span class="avocat_name">NAME </span></div>
<div class="type-avocat">Avocat postulant au Tribunal Judiciaire</div>
</div>
<div class="more-info">Plus d'informations</div>
</section>
<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom" style="display: none;" id="ui-id-2" aria-labelledby="ui-id-1" role="tabpanel" aria-hidden="true">
<div class="details">
<div class="detail-avocat-row ">
<div class="detail-avocat-content overflow-h">
<span>Structure :</span>
<div>
<p>Cabinet individuel NAME</p>
</div>
</div>
</div>
<div class="detail-avocat-row ">
<div class="detail-avocat-content overflow-h">
<span>Adresse :</span>
<div>
<p>21 rue Belle Isle 57000 VILLE</p>
</div>
</div>
</div>
<div class="detail-avocat-row ">
<div class="detail-avocat-content overflow-h">
<span>Mail :</span>
<div>
<p>cabinet#mail.fr</p>
</div>
</div>
</div>
<div class="detail-avocat-row">
<div class="detail-avocat-content overflow-h">
<span>Tél :</span>
<div>
<p>Telnum</p>
</div>
</div>
</div>
<div class="detail-avocat-row">
<div class="detail-avocat-content overflow-h">
<span>Fax :</span>
<div>
<p> </p>
</div>
</div>
</div>
<div class="contact-avocat"> Contacter </div>
</div>
</div>
</div>
And here is my python code:
divtel = self.driver.find_elements(by=By.XPATH,
value=f'//div[#class="detail-avocat-content overflow-h"]/div/p')#div[#class="detail-avocat-content overflow-h"]')
for p in divtel:
print(p.text)
It doesnt print anything...with other similar pages it prints the text but in this case it doesnt altough there is text in the nested span and div/p . Do you know why?
How can i resolve my problem please?
thank you
The method .text works only when the webelement containing the text is visible in the webpage. If otherwise the webelement is hidden, you have to use .get_attribute('innerText') or .get_attribute('textContent') or .get_attribute('innerHTML') (see here for difference between them). So for example change
print(p.text)
to
print(p.get_attribute('innerText'))

Remove parents of the elements in Python Selenium

The HTML is located below, If the span value is less than 20%, then I want to remove the span child up until the <div class="action"> parent only.
So for example:
<div class="item">
<div class="info">
<div class="action">
<div class="content">
<span class="content-name"> 5% </span>
</div>
</div>
</div>
</div>
From the above HTML, these code should only be removed:
<div class="action">
<div class="content">
<span class="content-name"> 5% </span>
</div>
</div>
So what should left is:
<div class="item">
<div class="info">
</div>
</div>
This is my current python code:
items = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[#class='content-name']")))
for item in items:
percentage_text = re.findall("\d+", item.text)[0]
if int(percentage_text) <= 20:
driver.execute_script("arguments[0].remove();", item)
But it only removes the span class and not its parent.
Here is the full HTML, I think it needs javascript to remove elements but I am very new on javascript I researched for more than 2 hours and I still can't find solutions. Thank you very much.
<div class="item">
<div class="info">
<div class="action">
<div class="content">
<span class="content-name"> 5% </span>
</div>
</div>
</div>
</div>
<div class="item">
<div class="info">
<div class="action">
<div class="content">
<span class="content-name"> 95% </span>
</div>
</div>
</div>
</div>
<div class="item">
<div class="info">
<div class="action">
<div class="content">
<span class="content-name"> 32% </span>
</div>
</div>
</div>
</div>
<div class="item">
<div class="info">
<div class="action">
<div class="content">
<span class="content-name"> 15% </span>
</div>
</div>
</div>
</div>
get to the parent of the parent:
driver.execute_script("arguments[0].parentElement.parentElement.remove();", item)

How to find specific element in list and its' button (Selenium Python)

I am trying to find an specific element with class='name' <h3Brook/h3> then click on the specific button inside of that div. Only care about "Brook" which is under one of the 'puppy_list'
def find_web_element(self, by_locator, element_text):
web_element=WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
if web_element == name:
return web_element
else:
pass
Then I want to find the button inside that element I found (brook) that's under class="rounded_button" type="submit" value="View Details" in that same div class with name Brook.
I'm able to do it by hardcoding the xpath but trying to find it by name:
self.homePage=HomePage(self.driver)
brook = (By.XPATH, '/html/body/div/div[1]/div[3]/div[2]/div/div[4]/form/div/input') #Hardcoded path for Brook
self.homePage.click_details(brook)
self.DetailsPage=DetailsPage(self.homePage.driver)
Where click is just click by_locator
ADDING HTML CODE:
<div class="puppy_list">
<div class="list_line_odd">
<div class="image"><img alt="Brook" class="list_image" src="/assets/Brook-ed6c0be3a8830921c5a954d1bc283354.jpg"></div>
<div class="name">
<h3>Brook</h3>
</div>
<div class="details">
<h4>Golden Retriever</h4>
<h4>Female</h4>
</div>
<div class="view">
<form action="/puppies/4" class="button_to" method="get">
<div><input class="rounded_button" type="submit" value="View Details"></div>
</form>
</div>
</div>
</div>
<div class="puppy_list">
<div class="list_line_even">
<div class="image"><img alt="Hannah" class="list_image" src="/assets/Hannah-8f4449876737fadb369a7c7bab7fb0da.jpg"></div>
<div class="name">
<h3>Hanna2</h3>
</div>
<div class="details">
<h4>Labrador Retriever Mix</h4>
<h4>Female</h4>
</div>
<div class="view">
<form action="/puppies/3" class="button_to" method="get">
<div><input class="rounded_button" type="submit" value="View Details"></div>
</form>
</div>
</div>
</div>
<div class="puppy_list">
<div class="list_line_odd">
<div class="image"><img alt="Maggiemae" class="list_image" src="/assets/MaggieMae-20224817b655d9dc556645ecf49c8d60.jpg"></div>
<div class="name">
<h3>Maggie Mae</h3>
</div>
<div class="details">
<h4>Border Colie Mix</h4>
<h4>Female</h4>
</div>
<div class="view">
<form action="/puppies/1" class="button_to" method="get">
<div><input class="rounded_button" type="submit" value="View Details"></div>
</form>
</div>
</div>
</div>
<div class="puppy_list">
<div class="list_line_even">
<div class="image"><img alt="Ruby_sue" class="list_image" src="/assets/ruby_sue-6a9a75d0fc2d367acca04c13390c3f7a.jpg"></div>
<div class="name">
<h3>Ruby Sue</h3>
</div>
<div class="details">
<h4>Pit Bull Terrier</h4>
<h4>Female</h4>
</div>
<div class="view">
<form action="/puppies/8" class="button_to" method="get">
<div><input class="rounded_button" type="submit" value="View Details"></div>
</form>
</div>
</div>
</div>
<br clear="all">
<p></p>
<div class="pagination"><span class="previous_page disabled">← Previous</span> <em class="current">1</em> <a rel="next" href="/agency/index?page=2">2</a> 3 <a class="next_page" rel="next" href="/agency/index?page=2">Next →</a></div>
<p></p>
</div>
You can find the info on 'Brook' based on the h3 tag
brook = driver.find_element_by_xpath("//h3[(text()= 'Brook')]")
Which will output <h3>Brook</h3>. You can get to the parent div by using xpath /... The parent div is <div class="name"><h3>Brook</h3></div>, so you want to move to the grandparent div by using /.. again.
brook = driver.find_element_by_xpath("//h3[(text()= 'Brook')]/../..")
Then, select your button by
click_button = brook.find_element_by_xpath("//input[#class='rounded_button']")

get content of <li> tags with beautifulsoup in python

I want to get content of first 3 <li> tag after the <section> tag, I don't know how to manipulate children tag in BeautifulSoap, I tried to get this strip the text and then get what I want with splitting it but I wasn't successful.
This is HTML code :
<section class="l-map">
<ul>
<li>خیابان شریعتی، روبروی پارک کوروش، کوچه پیروز، پلاک 48 </li>
<li>22855157 22852085</li>
<li>شریعتی:قلهک، سید خندان
</li>
</ul>
<div class="foot">
<a class="dm fancy" href="#contact" id="inline">پیام مستقیم به مدیر</a>
<a class="rm" href="#phonenumber" id="inline">دریافت پیامکی اطلاعات</a>
</div>
<input id="IsMaximumSmsReached" name="IsMaximumSmsReached" value="False" type="hidden">
<div style="display:none">
<div id="phonenumber">
<div class="contact-form number">
<h1>
دریافت پیامکی اطلاعات
<i class="icon contact"></i>
</h1>
<p>
شماره تلفن همراه خود را وارد کنید.
</p>
<form id="sendSMS">
<div class="form-input">
<input id="cellphone" name="cellphone" placeholder="برای مثال. 09121112222" type="text">
</div>
<div class="form-submit">
<button type="submit" href="#" class="submit">ارسال</button>
</div>
<p class="alert-box"></p>
</form>
</div>
</div>
</div>
<div style="display:none">
<div id="contact">
<div class="contact-form">
<h1>
ارسال پیام به مدیریت رستوران
<i class="icon message"></i>
</h1>
<p>
در این بخش شما می توانید به صورت مستقیم به مدیریت رستوران پیام ارسال نمایید.
<br>
پیام خود را در زیر بنویسید و ارسال نمایید.
</p>
<form id="managerMessage">
<div class="form-input">
<input id="MessageSenderName" name="MessageSenderName" placeholder="نام شما (اختیاری)">
<input id="MessageSenderPhone" name="MessageSenderPhone" placeholder="تلفن تماس شما (اختیاری)"><br>
<input id="MessageSenderEmail" name="MessageSenderEmail" placeholder="ایمیل شما (اختیاری)"><br>
<textarea id="MessageToManager" name="MessageToManager" placeholder="پیام"></textarea>
</div>
<div class="form-submit">
<button type="submit" href="#" class="submit">ارسال</button>
</div>
<p class="alert-box"></p>
</form>
</div>
</div>
</div>
</section>
I just can access the whole <section> tag with this line of code:
address = soup.find('section', class_="l-map")
I appreciate every help or comment you give me :)
You can use the function .find_all() to find all the li components inside the section , and then get its text by using either .text attribute or the method - .get_text() . Example -
>>> for lis in address.find_all('li'):
... print(lis.get_text())
...
<first li text>
22855157 22852085
<third li text>

Can't able to crawl the HTML data(elements view, not page view)?

I want to crawl the HTML data(elements view using Chrome Developer Tools, not page view).
I crawled HTML data in Python.
- The following is tried code in Python.
#-*- coding: utf-8 -*-
import requests
url = 'http://m.entertain.naver.com/comment/list?page=2&gno=news117%2C0002600716&sort=newest&aid=0002600716&oid=117'
response = requests.get(url)
print(response.content)
But only Page view was crawled.
- The following is crawled Page view using the above Python code.
...
<div id="social-comment">
<ul class="cmt_lst">
<li class="ld"><span>로딩중입니다.</span></li>
</ul>
</div>
...
I want to crawl the Elements view using Python.
- The following is Elements view using Chrome Developer Tools(I want to crawl the HTML data).
...
<div id="social-comment">
<div class="sc_cmt_wrp" queryid="C1431107741291317890" style="display: block;">
<div id="tabArea">
<ul class="cmt_tab">
<li style="width:50%" class="on">
댓글 <span class="_count">22</span>
</li>
<li>댓글쓰기</li>
</ul> </div>
<div id="sortOptionArea" style="display: block;">
<div class="cmt_choice">
<input type="radio" name="scmt-sort" id="newest" class="_scmt_sort(newest) _nclicks(rpt.rct)">
<label for="newest" title="최신순" class="_scmt_sort(newest) _nclicks(rpt.rct) on" onclick="javascript:;">최신순</label>
<input type="radio" name="scmt-sort" id="oldest" class="_scmt_sort(oldest) _nclicks(rpt.old)">
<label for="oldest" title="과거순" class="_scmt_sort(oldest) _nclicks(rpt.old)" onclick="javascript:;">과거순</label>
<input type="radio" name="scmt-sort" id="likability" class="_scmt_sort(likability) _nclicks(rpt.rcm)">
<label for="likability" title="호감순" class="_scmt_sort(likability) _nclicks(rpt.rcm)" onclick="javascript:;">호감순</label>
<input type="radio" name="scmt-sort" id="replycount" class="_scmt_sort(replycount) _nclicks(rpt.rpl)">
<label for="replycount" title="답글많은순" class="_scmt_sort(replycount) _nclicks(rpt.rpl)" onclick="javascript:;">답글많은순</label>
</div> </div>
<div id="noticeArea"></div>
<div id="commentListPaginationArea"> <div class="_noComments _refreshable"></div> <div class="_commentClosed _refreshable"></div> <div id="commentItemArea" class="_refreshable"><ul class="cmt_lst"> <li id="scmt-item-1149361" class=""> ldo1**** <p> 서인영 아직도 존예...하....자야는데..</p> <div class="func"> <span class="time">2015.04.28 오후 11:49</span> <span class="mobile">모바일에서 작성</span> | 신고 </div> <div class="btn_area2"> <div> 답글 <strong>0</strong> </div> <div> 10 7 </div> </div> </li> <li id="scmt-item-1149360" class=""> dbgh**** <p> 1빠 댓글수채우기.</p> <div class="func"> <span class="time">2015.04.28 오후 11:49</span> <span class="mobile">모바일에서 작성</span> | 신고 </div> <div class="btn_area2"> <div> 답글 <strong>0</strong> </div> <div> 1 5 </div> </div> </li> </ul></div> <div id="paginationArea" style="display: block;"> <div class="cmt_pg"> <span class="cmt_pg_prev">이전</span> <span class="cmt_pg_btn uc_vh scmt-page-prev-off" style="display: none;"><span class="cmt_pg_prev">이전</span></span> <em class="cmt_pg_pg _pageInfo">21 - 22 <span class="u_vc">페이지 </span><span class="cmt_pg_total">/ <span class="u_vc">총 </span>22<span class="u_vc"> 페이지</span></span></em> <span class="cmt_pg_btn uc_vh scmt-page-next-off" style="display: inline-block;"><span class="cmt_pg_next">다음</span></span> <span class="cmt_pg_next">다음</span> </div></div> </div></div></div>
...
I don't know about Java script. If possible, Give tell me the easy way(Python, wget & curl in Linux).

Categories