Using Excel VBA, I wish to scrape values from two Drop-down lists. One is filled with states' names, and another with cities.
I can scrape the states names, but when I try to scrape the cities names I get nothing. The cities list is filled accordingly to state selected.
How can I list every city on second drop-down list for each state in the first list?
This query gives me only state names and the default value of the second list:
Sub ScrapDropDown()
Const URL As String = "http://idebescola.inep.gov.br/ideb/consulta-publica"
Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
XMLPage.Open "GET", URL, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText
Set HTMLDocment = HTMLDoc.getElementById("pkCodEstado")
For i = 1 To HTMLDocment.Length - 1
Set HTMLpkCodMunicipio = HTMLDoc.getElementById("pkCodMunicipio")
For Each HTMLMun In HTMLpkCodMunicipio.getElementsByTagName("option")
Debug.Print i & "-" & HTMLDocment(i).Value & "-" & HTMLDocment(i).innerText & "-" & HTMLMun.Value & "-" & HTMLMun.innerText
Next HTMLMun
Next i
End Sub
Part of HTML with drop list i want to scrape (three dots I removed another unwanted lists), where selected (on site) a state from the first list, without selecting id="pkCodMunicipio" have only one option
<form method="post" name="frm" class="classForm" id="frm">
<label for="pkCodEntidade">Por Código</label>
<div class="divRequired">
</div>
<input name="pkCodEntidade" id="pkCodEntidade" placeholder="Código da Escola" title="Por Código" class="onlynumbers" maxlength="8" tabindex="15" type="text" value="">
<hr>
<label id="lbl">Por área de interesse</label>
<div id="lblDivRequired" class="divRequired" style="display: ;">
</div>
<select name="pkCodEstado" id="pkCodEstado" tabindex="16">
<option value="">UF</option>
<option value="12">ACRE</option>
<option value="27">ALAGOAS</option>
<option value="16">AMAPÁ</option>
<option value="13">AMAZONAS</option>
<option value="29">BAHIA</option>
<option value="23">CEARÁ</option>
<option value="53">DISTRITO FEDERAL</option>
<option value="32">ESPÍRITO SANTO</option>
<option value="52">GOIÁS</option>
<option value="21">MARANHÃO</option>
<option value="51">MATO GROSSO</option>
<option value="50">MATO GROSSO DO SUL</option>
<option value="31">MINAS GERAIS</option>
<option value="15">PARÁ</option>
<option value="25">PARAÍBA</option>
<option value="41">PARANÁ</option>
<option value="26">PERNAMBUCO</option>
<option value="22">PIAUÍ</option>
<option value="33">RIO DE JANEIRO</option>
<option value="24">RIO GRANDE DO NORTE</option>
<option value="43">RIO GRANDE DO SUL</option>
<option value="11">RONDÔNIA</option>
<option value="14">RORAIMA</option>
<option value="42">SANTA CATARINA</option>
<option value="35">SÃO PAULO</option>
<option value="28">SERGIPE</option>
<option value="17">TOCANTINS</option>
</select>
<select name="pkCodMunicipio" id="pkCodMunicipio" tabindex="17">
<option value="">Municípios</option>
<option value="1400050">ALTO ALEGRE</option>
<option value="1400027">AMAJARI</option>
<option value="1400100">BOA VISTA</option>
<option value="1400159">BONFIM</option>
<option value="1400175">CANTA</option>
<option value="1400209">CARACARAI</option>
<option value="1400233">CAROEBE</option>
<option value="1400282">IRACEMA</option>
<option value="1400308">MUCAJAI</option>
<option value="1400407">NORMANDIA</option>
<option value="1400456">PACARAIMA</option>
<option value="1400472">RORAINOPOLIS</option>
<option value="1400506">SAO JOAO DA BALIZA</option>
<option value="1400605">SAO LUIZ</option>
<option value="1400704">UIRAMUTA</option>
</select>
...
<button name="btnSearch" class="btnDefault btn btn-warning" title="Buscar" type="submit" id="btnSearch" onclick="void(0);">Buscar</button>
</div>
<input type="hidden" name="undefined" value="undefined">
</form>
You can use a css selector combination. The below use an id (#) selector to target the parent select tag element in descendant combination with option element selector to get all the child option tag elements.
Dim nodeList As Object, i As Long
Set nodeList = HTMLDoc.querySelectorAll("#pkCodEstado option")
For i = 0 To nodeList.Length-1
Debug.Print nodeList.item(i).innerText
Next
i is already declared at the top so you don't actually need to declare again. You should use Option Explicit at the top off all modules and thus declare all your variables. You have a number of undeclared variables in your code.
Related
I have the following select values in my upload.ejs file:
<div class="form-group">
<label>Category</label>
<select class="form-control" name="category" action="/readCategory" method="post" >
<option value="Agriculture, Food & Natural Resources">Agriculture, Food & Natural Resources</option>
<option value="Architecture & Construction">Architecture & Construction</option>
<option value="Arts, Audio/Video Technology & Communications">Arts, Audio/Video Technology & Communications</option>
<option value="Business Management and Administration">Business Management and Administration</option>
<option value="Education and Training">Education and Training</option>
<option value="Engineering">Engineering</option>
<option value="Gaming">Gaming</option>
<option value="Government and Public Administration">Government and Public Administration</option>
<option value="Health Science">Health Science</option>
<option value="Hospitality and Tourism">Hospitality and Tourism</option>
<option value="Human Services">Human Services</option>
<option value="IT (Information Technology)">IT (Information Technology)</option>
<option value="Law, Public Safety, Corrections & Security">Law, Public Safety, Corrections & Security</option>
<option value="Manpower & Services">Manpower & Services</option>
<option value="Manufacturing">Manufacturing</option>
<option value="Marketing, Sales & Services">Marketing, Sales & Services</option>
<option value="Science, Technology & Mathematics">Science, Technology & Mathematics</option>
<option value="Transportation, Distribution & Logistics">Transportation, Distribution & Logistics</option>
</select>
</div>
I need to read these values & display them as a heading in my index.ejs file:
<div class="container my-4">
<h2><%= %></h2>
I have tried it out in many ways like in my server.js like:
app.post("/readCategory", function(request, result){
result.render("index", {
"categories": request.body.category
});
});
and used categories in my h2 heading in index.html file but nothing seems to work.
Does somebody have a solution for this?
I am trying to make a form in Angular with a select box whose values are taken from an array. I also need the form to show inputs based on the value of that select. This is the code:
<select #freq formControlName="freq" class="m-2">
<option disabled>Select</option>
<option value="">Select Option</option>
<option *ngFor="let x of freq_list" [ngValue]="x">{{x}}</option>
</select>
<div *ngIf="freq.value!=''">
{{freq.value}}
<div *ngFor="let x of parameter_config[freq.value]">
<input type="number" formControlName="input1">
</div>
</div>
where freq_list is a simple string[]=['option1',option2',option3'] and parameter_config is a map {[id:string]: string[];}where the id is the a value of freq_list.
And this is the form creation:
form=this.fb.group({
freq:'',
});
Everything works except that the value returned by the selected option is not simply 'option1' but it is preceded by the order in the list (e.g. '1: option1', '2: option2', ...)
Is there something I am missing? How is this the default behavior?
replace ngValue with value
<option *ngFor="let x of freq_list" [ngValue]="x">{{x}}</option>
should be
<option *ngFor="let x of freq_list" [value]="x">{{x}}</option>
I am trying to leverage Selenium to automate standardized web inputs; however, the remaining piece is handling the calendar popup. I am needing to dynamically set the values of the calendar to the relative cell value in the sheet.
I am using my windows computer- Excel-VBA; using Selenium Webdriver and Chromedriver. I've tried using both Xpaths-Method, ExecuteScript (JS), and controlling mouse.
Sub googStart()
Dim bot As New WebDriver
Dim rng As Range
Dim ele As WebElement
'
lastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
bot.Start "chrome", "URL HERE"
bot.Get "/"
For X = 2 To lastRow
Set ele = bot.FindElementById("calendarId").AsSelect
ele.SelectByValue "30"
' Above threw an runt time error 59
bot.FindElementById("calendarId").Click
' this opens up the calender's pop up
bot.Wait 500
Also, tRied the below combinations:
' cScript = "document.getElementsByClass('a-cal-days').value='29'"
' bot.ExecuteScript cScript
' For Each ele In bot.FindElementsByXPath("//select[#class='a-cal-select-day[]']")
' ele.AsSelect = Sheet1.Cells(X, 4)
' bot.FindElementById("calendarInputId").Value ("10/29/2019")
' bot.FindElementByXPath("(//[#class='a-cal-d'])").Text Sheet1.Cells(X, 5)
' bot.findElement(By.xpath("//input[#id='calendarId']/div[1]/select[2])).setAttribute('value', '24')
' //*[#id="calendarId"]/div[1]/select[1] ' Month
' //*[#id='calendarId']/div[1]/select[2] ' day select
' //*[#id="calendarId"]/div[1]/select[2]
Cells Values; needing to match calendar day and month: See attached image!
HTML BELOW:
<td>
<div id="calendarId" data-a-cal-attributes="{"minDate":{"month":10,"year":2019,"day":23},"maxDate":{"month":10,"year":2020,"day":24},"selectedDate":{"month":10,"year":2019,"day":24}}"
data-a-cal-input="true" class="a-cal-calendar-container a-cal-static">
<div class="a-cal-dropdown-container">
<select data-action="a-cal-dropdown-select" class="a-cal-select-month a-declarative">
<option value="">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select data-action="a-cal-dropdown-select" class="a-cal-select-day a-declarative">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select data-action="a-cal-dropdown-select" class="a-cal-select-year a-declarative">
<option value="">Year</option>
<option value="1900">1900</option>
<option value="1901">1901</option>
<option value="1902">1902</option>
<option value="1903">1903</option>
<option value="1904">1904</option>
<option value="1905">1905</option>
<option value="1906">1906</option>
<option value="1907">1907</option>
<option value="1908">1908</option>
<option value="1909">1909</option>
<option value="1910">1910</option>
<option value="1911">1911</option>
<option value="1912">1912</option>
<option value="1913">1913</option>
<option value="1914">1914</option>
<option value="1915">1915</option>
<option value="1916">1916</option>
<option value="1917">1917</option>
<option value="1918">1918</option>
<option value="1919">1919</option>
<option value="1920">1920</option>
<option value="1921">1921</option>
<option value="1922">1922</option>
<option value="1923">1923</option>
<option value="1924">1924</option>
<option value="1925">1925</option>
<option value="1926">1926</option>
<option value="1927">1927</option>
<option value="1928">1928</option>
<option value="1929">1929</option>
<option value="1930">1930</option>
<option value="1931">1931</option>
<option value="1932">1932</option>
<option value="1933">1933</option>
<option value="1934">1934</option>
<option value="1935">1935</option>
<option value="1936">1936</option>
<option value="1937">1937</option>
<option value="1938">1938</option>
<option value="1939">1939</option>
<option value="1940">1940</option>
<option value="1941">1941</option>
<option value="1942">1942</option>
<option value="1943">1943</option>
<option value="1944">1944</option>
<option value="1945">1945</option>
<option value="1946">1946</option>
<option value="1947">1947</option>
<option value="1948">1948</option>
<option value="1949">1949</option>
<option value="1950">1950</option>
<option value="1951">1951</option>
<option value="1952">1952</option>
<option value="1953">1953</option>
<option value="1954">1954</option>
<option value="1955">1955</option>
<option value="1956">1956</option>
<option value="1957">1957</option>
<option value="1958">1958</option>
<option value="1959">1959</option>
<option value="1960">1960</option>
<option value="1961">1961</option>
<option value="1962">1962</option>
<option value="1963">1963</option>
<option value="1964">1964</option>
<option value="1965">1965</option>
<option value="1966">1966</option>
<option value="1967">1967</option>
<option value="1968">1968</option>
<option value="1969">1969</option>
<option value="1970">1970</option>
<option value="1971">1971</option>
<option value="1972">1972</option>
<option value="1973">1973</option>
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
<option value="2025">2025</option>
<option value="2026">2026</option>
<option value="2027">2027</option>
<option value="2028">2028</option>
<option value="2029">2029</option>
<option value="2030">2030</option>
<option value="2031">2031</option>
<option value="2032">2032</option>
<option value="2033">2033</option>
<option value="2034">2034</option>
<option value="2035">2035</option>
<option value="2036">2036</option>
<option value="2037">2037</option>
<option value="2038">2038</option>
<option value="2039">2039</option>
</select>
</div>
<div class="a-cal-date-range-group a-cal-date-range-group-vertical">
<div class="a-cal-date-range-wrapper">
<div class="a-input-text-addon-group-wrapper">
<div class="a-input-text-addon-group a-width-medium"><span class="a-input-text-addon"><i class="a-icon a-icon-calendar"></i></span><input type="text" maxlength="10" data-action="a-cal-input" data-input="start" class="a-input-text a-cal-input a-declarative" id="calendarInputId" /></div>
</div>
</div>
</div>
</div>
</td>
You need to ensure that it is the select element which serves as the base. Also, I don't think you can grab the window handle of pop-up and use that to switch to pop-up but you could try bot.SwitchToNextWindow. You return to original with bot.SwitchToDefaultContent.
E.g.
bot.SwitchToNextWindow
Dim monthSel As Object, daySel As Object
Set monthSel = bot.FindElementByCss(".a-cal-select-month").AsSelect
Set daySel = bot.FindElementByCss(".a-cal-select-day").AsSelect
Set yearSel = bot.FindElementByCss(".a-cal-select-year").AsSelect
Then I would expect you to be able to leverage SelectBy methods e.g.
''<== assuming cell contains value 1
monthSel.SelectByText "10" 'or monthSel.SelectByText ActiveSheet.Cells(2,1).value
daySel.SelectByValue "29"
yearSel.SelectByValue 2019 'different ways of doing same thing shown
Be careful with your current lastRow calculation and looping method. The AsSelect can be done outside any loops as you don't need to set new references (at least I don't think so); you only want to alter the currently selected value in the dropdowns.
Hello everyone I'm starting with python + beautufulsoup4 + requests and need to remove the tags span without any id or class cut to extract the number of pages I need to go or scrap of this fragment of code page I just need to remove the
<div class="pagination">
<select id="CurrentPage" data-val="true" data-val-number="The field CurrentPage must be a number." data-val-required="The CurrentPage field is required." name="CurrentPage">
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
</select>
<span>of 28</span>
<a class="btn next" href="/listings/trucks/for-sale/list/category/27/trucks/manufacturer/international/model-group/9400?page=2">Next »</a>
</div>
import re
span_tag = soup.find(name='span',text=re.compile(r'of \d+'))
page_num = span_tag.text.rstrip('of ')
I have two select list that are equal how do i access the second one?
I have put a value on the first one but when putting a value to the second it goes to the first.
Anyone can help me.
<div id="selectNumber">
<select id="numbers" name="numbers" tabindex="5" onchange="changeNumbers(this);">
<option value="" selected="selected"></option>
<option value="all">25</option>
<option value="123">26</option>
<option value="124">27</option>
</select>
<a onclick="addRemoveSelectedNumber(this);">
<img src="../../static/images/delete.png">
</a>
</div>
<div id="selectNumber">
<select id="numbers" name="numbers" tabindex="5" onchange="changeNumbers(this);">
<option value="" selected="selected"></option>
<option value="all">25</option>
<option value="123">26</option>
<option value="124">27</option>
</select>
<a onclick="addRemoveSelectedNumber(this);">
<img src="../../static/images/delete.png">
</a>
Tanks
This will select 25 from the second select list:
browser.select_list(:id => "numbers", :index => 1).select "25"
More information: Multiple Attributes chapter at https://github.com/zeljkofilipin/watirbook/blob/master/link.md
By the way, you should report a bug that there are multiple instances of HTML elements with the same id on the page. Per the HTML standard, ID values should be unique