bs4 getting a tag's value - beautifulsoup

Here's html code I'm working on.
<div class="input-group m-b">
<span class="input-group-addon">
$
</span>
<input class="form-text form-control input-lg-3" disabled="disabled"
groupfields="$" id="edit-transfer--3" maxlength="128"
name="transfer_d" size="60" type="text" value="71"/>
</div>
What I want is to get "71" which is the value of a "value" tag
I've tried
elem = soup.find('input', attrs={'id':'edit-transfer--3'})
print(elem)
and gives
<input class="form-text form-control input-lg-3" disabled="disabled" groupfields="$" id="edit-transfer--3" maxlength="128" name="transfer_d" size="60" type="text" value="71"/>
and I'm stuck
print(elem.find('value') gives me None
and
print(elemd.find('value').get_text())
gives me an error
AttributeError: 'NoneType' object has no attribute 'get_text'
How can I extract the value from the tag?

Try this:
from bs4 import BeautifulSoup
html = '''<div class="input-group m-b">
<span class="input-group-addon">
$
</span>
<input class="form-text form-control input-lg-3" disabled="disabled"
groupfields="$" id="edit-transfer--3" maxlength="128"
name="transfer_d" size="60" type="text" value="71"/>
</div>'''
soup = BeautifulSoup(html, "html.parser")
elem = soup.find('input', attrs={'id':'edit-transfer--3'})
print(elem['value'])
returns
71
The find method is used to find child elements from the parent. Since value is an attribute and there's no element tag called value, None is returned.
The get_text method will only extract the innerText of the element. Since the previous find returned None, it throws the error.
To get a specific attribute you need to use square brackets.

Related

How to write following sibling CSS/Xpath expression of username field?

HTML Code:
<form autocomplete="on" xpath="1">
<div class="IiD88i _351hSN">
<input class="_2IX_2- VJZDxU" autocomplete="off" type="text" value="">
<span class="_36T8XR"></span>
I can write this one : //input[#type='text']
But how to write it in "following sibling" CSS/xpath of the above code?
If you want to fetch input node based on its sibling, try:
//label[.='Enter Email/Mobile number']/preceding-sibling::input

Unable to get text from disabled textbox

I need to print value of a disabled text box. The below given is the source code
<div class="col-6 col-md-2 form-group mb-2" xpath="1">
<input name="SMDetailsViewModel.SoRSummaryRowID" class="form-control rum-disabled" id="SRowID" type="hidden" value="3908" tabindex="-1">
<input name="SMDetailsViewModel.ID" **value**="20445" class="form-control rum-disabled " id="SID" type="text" tabindex="-1">
</div>
The 'value' needs to get printed and it is dynamic value.
I have tried with CSS, Xpath like below
WebElement SoR=driver.findElement(By.cssSelector("#SID")); With xpath as well
String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);
tried with GetAttribute as well
Working with below
WebElement SoR=driver.findElement(By.cssSelector("#SID"));
String SoRID=SoR.getAttribute("value");
//String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);

How to access nested input attribute using Jsoup

Please could You help my with selecting int value from input attr"value"? In this case "12".
<td class="fit-content">
<div class="add-cart" data-original-title="Ilość w op. zbiorczym: 12 szt.">
<div class="input-group">
<input class="form-control form-control-sm" type="text" value="12">
</div>
</div>
Try
String value = doc.selectFirst("div.add-cart div.input-group input").attr("value");

Selenium/Python - Element not reachable by keyboard

I am trying to fill in a form automatically. I have recorded a script with Selenium.
One of the field to populate is the zip code. When I start typing the code, a new window opens to suggest appropriate option (javascript autofill)
I need to select the first item the ul (cf. html below)
I am quite new to Selenium and though I have been reading the Selenium/html documentation I am totally stuck for almost 1 month on this...
Many thanks in advance for your support
My code is as follows and I received the error message "Element is not reachable by keyboard"
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "selected")))
first_option.send_keys(Keys.RETURN)
**HTML**
<div id="localisation_left">
<div class="line toDisable">
<label for="location_p" class="label">Ville ou code postal *</label>
<div class="field-wrapper location-container">
<div class="inputWrapper">
<i id="browserGeoloc" class="icon-geoloc icon-2x blue"></i>
<div class="loaderGif-small hidden"></div>
<input class="nude" name="location_p" id="location_p" autocomplete="off" value="Paris 75010" type="text">
<input name="zipcode" value="" type="hidden">
<input name="city" value="" type="hidden">
<script type="text/javascript">
var numberOfLocation = 1, numberOfAuthorizedLocation = 1;
var cityNewadMultipleLocation = new MultipleLocationNewad('input[name="location_p"]', numberOfLocation, numberOfAuthorizedLocation);
cityNewadMultipleLocation.cityAndZipcodeAreSelected = true;
</script>
<input name="region" value="" type="hidden">
<input name="dpt_code" value="" type="hidden">
</div>
<ul class="location-list visible" style="top: 43px;">
<li data-region="12" data-dpt-code="75" class="selected">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75011</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75015</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75009</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75010</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75017</span>
</li>
You can click on the first option, instead of pressing Enter key
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
condition = EC.visibility_of_element_located((By.CSS,
"label[for='location_p'] + div ul.location-list > li"))
first_option = WebDriverWait(driver, 15).until(condition)
first_option.click()
I had a similar issue, and the above solution did not work for me (it would throw an invalid syntax error).
I first used the find_element_by_css_selector function, which selects the first occurrence of the element with given attributes. This did not work.
Then I used the find_elements_by_css_selector (notice the s), which returns a list of the elements with given attributes. There were two elements in that list. Of course the first one (with index [0]) was not accessible by keyboard: this is equivalent of doing (1) above. But the second element (with index [1]) was accessible by keyboard.
Problem solved.
Try selecting by using Xpath below
elem = driver.find_element_by_id("location_p") elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.Xpath,
".//*[#id='localisation_left']/div/div/ul/li[1]")))
first_option.click()
If anyone faces Element not reachable by keyboard issue, one can also seek below approach:
input_xpath = '//input[#type="file"][#name="files[]"][#class="class_name"]'
input_element = self.driver.find_element_by_xpath(input_xpath)
## to make element visible:
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
input_element)
input_element.send_keys('~\Desktop\Release2.pdf')

Inputs and its attributes are constantly changing in Dynamic x-path in Selenium

I tried to find the x-path of a text field username and password, but it keeps on changing dynamically.I won't be able to use starts-with or contains in a findelement using x-path. Here's the HTML DOM-
<div id="contents">
<h1 style="white-space:pre; width:80px; float:left;line-height:35px;">Login</h1>
<span style="float:left; padding-top:13px; COLOR:#990033; font-weight:bold;"> Student | Parent | Faculty</span>
<div class="form-elements">
<div class="form-elements">
<div class="form-elements">
<div class="label-txt">Password</div>
<input id="rcnr2uew1m0rkikeaaniwk" type="password" style="display:none;" name="rcnr2uew1m0rkikeaaniwk"/>
<input id="ko2xs123ebqyoluh15bulu" type="password" style="display:none;" name="ko2xs123ebqyoluh15bulu"/>
<input id="cuouek4bfz41etm4hroj0r" type="password" style="display:none;" name="cuouek4bfz41etm4hroj0r"/>
<input id="u2ta3gv2o2ce0azx5plpuh" type="password" name="u2ta3gv2o2ce0azx5plpuh"/>
<input id="g03nwjuzhqnkuwgsl4q2mu" type="password" style="display:none;" name="g03nwjuzhqnkuwgsl4q2mu"/>
<input id="gddwv4z3amojk0yvoxi2v4" type="password" style="display:none;" name="gddwv4z3amojk0yvoxi2v4"/>
<input id="kxecmkho2vf1vcfb42icjr" type="password" style="display:none;" name="kxecmkho2vf1vcfb42icjr"/>
<span id="ctl04" style="color:Red;visibility:hidden;">*</span>
</div>
I tried to find the input[4] with no style.
Absolute x-path- html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[4]
Next time how it changes-
Absolute x-path- html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[17]
id and name of the input also keeps on changing without any common trait
You can do it by locating sibling of the User name label that is displayed, i.e. without attribute style="display:none;"
User Name
"//div[contains(text(), 'User Name')]/following-sibling::input[not(#style='display:none;')]"
Password
"//div[contains(text(), 'Password')]/following-sibling::input[not(#style='display:none;')]"
Or something similar using type attribute:
//input[#type='password'][not(#style)]