Selenium Python No Name or ID - python

How do I submit the postcode eg. "3000" and click submit Selenium (Python) when I can't search by the name or ID.
Here is the HTML
<div class="container-fluid" id="basicpage">
<div class="container">
<div class="row">
<div id=right_col class="col-xs-12 col-md-9">
<div class=nothingwrapper>
</div>
<div class=nothingwrapper>
<h1>Postcode Snapshot </h1>
<p> Please type in the postcode for which you would like to download a postcode snapshot:</p>
<form action="postcodesnapshotchoosepostcode.php" method="post">
<input name=sub value=1 type=hidden>
<p><input type=text name=postcode size=5></p><p><input type=submit value='Continue' /></p>
</form>
<p> </p>
</div>
Here is what I have tried
# Select the by name
postcode_box = driver.find_element(By.NAME, "postcode")
# Send information
postcode_box.send_keys('3000')
driver.find_element(By.NAME("sub")).submit()

//input[#value='Continue']
If you need an xpath for an attribute you can do it like so

Related

How to fetch a specific value from a HTML using BeautifySoup

I am trying to bring the percent value from a HTML file. I used the below two method in juypter Notebook and got the result as expected in both the method. But while I am trying to replicate the same in pycharm I am not getting expected result in either of the method. I am getting None in second method and picking another word in the first method
spans = soup.select_one('span').text
print("spans:", spans)
>> spans 99%
spans = soup.find("span", {"class": "rc_late"}).text
print("spans", spans)
>> spans 99%
Here is snipper of the HTML. Is there any way we can fetch those value (99%)?
<div class="content">
<h1>Latest report:
<span class="rc_late">99%</span>
</h1>
<aside id="help_panel_wrapper">
<input id="help_panel_state" type="checkbox">
<label for="help_panel_state">
<img id="keyboard_icon" src="keybd_closed.png" alt="Show/hide keyboard shortcuts">
</label>
<div id="help_panel">
<p class="legend">Shortcuts on this page</p>
<div class="keyhelp">
<p>
<kbd>n</kbd>
<kbd>s</kbd>
<kbd>m</kbd>
<kbd>x</kbd>
<kbd>c</kbd>
change column sorting
</p>
<p>
<kbd>[</kbd>
<kbd>]</kbd>
prev/next file
</p>
<p>
<kbd>?</kbd> show/hide this help
</p>
</div>
</div>
</aside>
<form id="filter_container">
<input id="filter" type="text" value="" placeholder="filter...">
</form>
<p class="text">
created at 2023-01-22 12:01 +0000
</p>
</div>
Here is one way to do it, given your example:
from bs4 import BeautifulSoup as bs
html = '''
<div class="content">
<h1>Latest report:
<span class="rc_late">99%</span>
</h1>
<aside id="help_panel_wrapper">
<input id="help_panel_state" type="checkbox">
<label for="help_panel_state">
<img id="keyboard_icon" src="keybd_closed.png" alt="Show/hide keyboard shortcuts">
</label>
<div id="help_panel">
<p class="legend">Shortcuts on this page</p>
<div class="keyhelp">
<p>
<kbd>n</kbd>
<kbd>s</kbd>
<kbd>m</kbd>
<kbd>x</kbd>
<kbd>c</kbd>
change column sorting
</p>
<p>
<kbd>[</kbd>
<kbd>]</kbd>
prev/next file
</p>
<p>
<kbd>?</kbd> show/hide this help
</p>
</div>
</div>
</aside>
<form id="filter_container">
<input id="filter" type="text" value="" placeholder="filter...">
</form>
<p class="text">
created at 2023-01-22 12:01 +0000
</p>
</div>
'''
soup = bs(html, 'html.parser')
data_we_want = soup.select_one('span[class="rc_late"]').text
print(data_we_want)
Result in terminal:
99%
You can find BeautifulSoup documentation here.

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']")

Custom Bootstrap Checkbox in Jinga2/Flask

I have the following WTF form
class config_reports(FlaskForm):
test2use = SelectMultipleField('Choose Test(s):', [DataRequired()], choices=[('', '')])
display_cuts = BooleanField('Display Proficiency Cuts?', default = True)
submit = SubmitField('Run Report')
and now I'm trying to fancy up my checkbox to use the example found here, https://getbootstrap.com/docs/4.0/components/forms/, which uses this code
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
Within my jinga2 template file, I'm trying the following. Some of the style is applied, but my checkbox isn't clickable. Any advice on how to properly apply the styling here?
<div class = 'col'>
<div class="formwrapper">
<form method="POST" action="{{url_for('config_tools.config_reader')}}">
<div class="form-field">
{{form2.test2use.label}}
{{form2.test2use (class="form-control", required=False)}} <br>
<div class="custom-control custom-checkbox">
{{form2.display_cuts.label(class="custom-control-label")}}
{{form2.display_cuts (class="custom-control-input") }}<br>
</div>
{{form2.submit(class = "btn btn-primary")}} <br><br>
</div>
</form>
</div>
</div>

log in to a website using Python's Requests module doesn't work on this site but work on other site

use the following code on twitter and github it work fine but it doesn't work on the main site i am using it for, can someone please tell me what went wrong.
i did not get any error instead it scrap back the login page instead of log me.
import requests
session = requests.Session()
params = {'j_username': '**********', 'j_password': '************'}
r = requests.post("https://connect.data.com/loginProcess", params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------")
print("Going to profile page...")
r = requests.get("https://connect.data.com/home")
print(r.text)
I am new to this so i can't figure out what went wrong, i have try many answer out before asking the question but none seem to workout for the site.
The login url is /login but the file that the login is been process is /loginProcess that is why i use /loginProcess but loginProcess does not print out my cookie but /login does print it out
The form to the site look like this:
<form id="command" name="LoginForm" action="https://connect.data.com/loginProcess" method="post">
<div>
<div class="login-container fields float-left">
<div class="content">
<div class="first">
<span class="title">Login</span>
</div>
<div class="middle">
<input id="j_username" name="j_username" type="email" class="text" placeholder="Email" maxlength="128" tabindex="1">
</div>
<div class="middle">
<input id="j_password" name="j_password" type="password" class="text" placeholder="Password" autocomplete="off" tabindex="2" maxlength="128">
</div>
<div class="middle">
<label for="_spring_security_remember_me" class="general-checkbox-label">
<input name="_spring_security_remember_me" tabindex="3" value="on" id="_spring_security_remember_me" class="checkbox margin-0px" type="checkbox">
<span>Keep me logged in </span>
</label>
</div>
<div class="last">
<button id="login_btn" type="submit" class="button-standard button-primary" tabindex="4">
<span class="button-standard-text">Login</span>
</button>
<a class="link" href="https://connect.data.com/forgotpassword" onclick="var x=".tl(";s_objectID="https://connect.data.com/forgotpassword_1";return this.s_oc?this.s_oc(e):true">Forgot Password?</a>
</div>
</div>
</div>
<div class="login-container marketing-message float-right">
<div class="content">
<h2>
Don't have an account?
Sign up now - it's FREE!
</h2>
<div class="float-left">
<img class="login-bicons" src="./Data.com Connect Business Contact Directory of Business Contacts and Company Information_files/clear.cache.gif">
</div>
<div class="float-left margin-left-20px">
<p>
<span id="pageTitle" style="color: #146791">Find</span>
business card information & B2B professionals.
</p>
<p>
<span id="pageTitle" style="color: #87c540">Research</span>
people & companies.
</p>
<p class="margin-top-5px">
<span id="pageTitle" style="color: #f37521">Get</span>
millions of contacts.
</p>
</div>
<div class="clear"></div>
</div>
</div>
<div class="clear nonlogged-container-foot">
<span class="new-to-ddc">New to Data.com?</span>
<a class="sign-up" href="https://connect.data.com/registration/signup" onclick="var x=".tl(";s_objectID="https://connect.data.com/registration/signup_1";return this.s_oc?this.s_oc(e):true">Sign up for an account...</a>
</div>
</div>
<input type="hidden" name="CSRF_TOKEN" id="CSRF_TOKEN" value="ce56932e08fc97bc29c5f6535b572664a66000513143584504b0ee7c66ef9659"></form>

Receiving HTML request from Bootstrap <button>

<form method="post" class="form-horizontal" role="form">
<div class="btn-group col-sm-3">
<button id="typeSelect" type="button" name="reportType" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
People <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>
...
</li>
...
</ul>
</div>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">Name</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputName" placeholder="Name" name="name" value="{{name}}" onblur="isEmpty(this)">
</div>
</div>
</form>
I have two inputs in my form and I am trying to get the input in my Python code.
report_type = self.request.get('reportType')
name = self.request.get('name')
name is working correctly, but report_type will always be None.
What is the correct way to retrieve the selection in a button (Bootstrap)?
According to the button element specifications:
A button (and its value) is only included in the form submission if the button itself was used to initiate the form submission.
You will have to find another way to pass the value of the button. You could, for example, store the value in a hidden field.

Categories