Selenium Web Driver Selecting option from Drop down - excel

I've got this HTML
<div>
<div tabindex="0" class="dropdown xs-dropdown show" id="selBudgetYearRange-wrap" style="border: 1px solid rgb(206, 212, 218); border-image: none;">
<button tabindex="-1" class="btn btn-default dropdown-toggle" id="selBudgetYearRange-btn" aria-expanded="true" aria-haspopup="true" type="button" data-toggle="dropdown">2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)
</button>
<div class="dropdown-menu show" id="selBudgetYearRange-dmenu" aria-labelledby="selBudgetYearRange-btn" style="left: 0px; top: 0px; width: calc(100% + 2 * 1px); margin-top: calc(0px + 1px); margin-left: calc(-0px - 1px); position: absolute; transform: translate3d(0px, 21px, 0px);" x-placement="bottom-start">
<a class="dropdown-item active" onclick="dropdownChanged(this, 'selBudgetYearRange','201910-202003,2');;return false;" href="javascript:void(0)">2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)</a>
<a class="dropdown-item" onclick="dropdownChanged(this, 'selBudgetYearRange','201810-201909,2');;return false;" href="javascript:void(0)">2019 vs 2018 (Whole Year Oct - Sep)</a>
<a class="dropdown-item" onclick="dropdownChanged(this, 'selBudgetYearRange','201710-201809,2');;return false;" href="javascript:void(0)">2018 vs 2017 (Whole Year Oct - Sep)</a>
</div><input name="selBudgetYearRange" id="selBudgetYearRange" type="hidden" value="201910-202003,2"></div>
</div>
which has 3 options
2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)
2019 vs 2018 (Whole Year Oct - Sep)
2018 vs 2017 (Whole Year Oct - Sep)
I've managed to work out that by using
.FindElementByXPath("//*[text()='2019 vs 2018 (Whole Year Oct - Sep)']").Click
I can select the middle option, however it only works if I click on the drop down first so that the options are visible.
I can't seem to get selenium to click on the drop down for me and make the options visible so that I can use the above xpath to select my desired option.
Please can someone advise on how to do this? Or if there's a way of selecting my options without needing to get the drop down to appear first?

So managed to get it to work.
by using clickdouble (after figuring out the xpath)
I have no idea why I need to use clickdouble instead of click.
But it works, so I'm not going to complain too much

Related

python selenium selection option from dropdown

I need help to select the option "Last 7 days" from the following dropdown using python and selenium:
here is the html for part of the dropdown:
<div class="date-range" id="yui_3_18_1_1_1614002255126_175" style="margin-right: 25px;">
<div class="option-select global default" id="yui_3_18_1_1_1614002255126_174">
<div class="select-open" id="yui_3_18_1_1_1614002255126_173">
<div class="select-title" id="yui_3_18_1_1_1614002255126_172">
<span class="option-title">DATE RANGE:</span>
<span class="option-selection" id="yui_3_18_1_1_1614002255126_170">Last 30 days</span>
</div>
<div class="dropdown-arrow"></div>
</div>
<div class="select-body" id="yui_3_18_1_1_1614002255126_190">
<div class="option">
Today
<span class="extra-info-wrapper">
<span> (</span>
<span class="extra-info">22 Feb</span>
<span>)</span>
</span></div>
<div class="option">
Yesterday
<span class="extra-info-wrapper">
<span> (</span>
<span class="extra-info">21 Feb</span>
<span>)</span>
</span></div>
<div class="option">
This week
<span class="extra-info-wrapper">
<span> (</span>
<span class="extra-info">Monday - Today</span>
<span>)</span>
</span></div>
<div class="option" id="yui_3_18_1_1_1614002255126_189">
Last 7 days
<span class="extra-info-wrapper">
<span> (</span>
<span class="extra-info">16 Feb - Today</span>
<span>)</span>
</span></div>
<div class="option">
This month
<span class="extra-info-wrapper">
<span> (</span>
<span class="extra-info">1 Feb - Today</span>
<span>)</span>
</span></div>
My code so far is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
myUsername="Xxx”
myPassword="Xxx”
driver.get("https://uk.ixl.com/signin/sop")
driver.find_element_by_xpath('//*[#id="siusername"]').send_keys(myUsername)
driver.find_element_by_xpath('//*[#id="sipassword"]').send_keys(myPassword)
driver.find_element_by_xpath('//*[#id="custom-signin-button"]').click()
time.sleep(1)
#select report
driver.get("https://uk.ixl.com/analytics/students-quickview?teacherId=125756982")
time.sleep(5)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'date-range'))).click()
driver.find_element_by_id('yui_3_18_1_1_1614002255126_189').click()
I am getting the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"css selector","selector":"[id="yui_3_18_1_1_1614002255126_189"]"}
This is only my second python project so forgive any lack of understanding, this is just a hobby, but I've been stuck for 2 days trying all sorts but nothing works, any help would be appreciated (even links to videos that would help solve this so I can learn), thanks
You can hard code text in xpath like this:
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'date-range'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//div[text()="Last 7 days"]'))).click()
Please change the text if you want other options.
What I usually do is:
driver.find_element_by_xpath(f'//select[#name="nameOfYourButton"]/option[text()=""TextToBeSelected"]').click()
Where nameOfYourButton and TextToBeSelected must be replaced with your specific variable
In particular, I can see that TextToBeSelected should be Last 7 days while from the HTML snippet that you are showing I can't see the name of the button.
Use WebDriverWait() and following xpath to click on Last 7 days.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='select-body']//div[#class='option' and contains(.,'Last 7 days')]"))).click()

How do I select radio buttons in IE using vba?

I am trying to select radio buttons and select value in selectCls but facing problem with my code please help me on the same
Thanks in advance..
My VBA Code is:
Browser.document.getElementById("ddlCycleID").SelectCls = "Cycle 274 (21 Jun 2017 - 31 Jul 2017)"
Browser.document.getElementById("chkproc").Checked = True
Browser.document.getElementById("btnSubmit").Click
And HTML Source code is:
<td>
<input value="chkproc" name="Module" type="radio" id="chkproc" checked="checked" tabindex="1" onclick="toggle(this)"> Processing Module
<td>
<td>
<input value="chkquery" name="Module" type="radio" id="chkquery" tabindex="2" onclick="toggle(this)"> Other Modules
<td>
<td>
<select name="ddlCycleID" id="ddlCycleID" tabindex="4" class="SelectCls" style="width:180px;">
<option value="Select Cycle">Select Cycle</option>
<option value="274">Cycle 274 (21 Jun 2017 - 31 Jul 2017)</option>
<option value="273">Cycle 273 (29 Jun 2017 - 29 Jun 2017)</option>
</select>
<td>
getting below error msg
Run-time error '424'
object required
in this code:
Browser.document.getElementById("ddlCycleID").SelectCls = "Cycle 274 (21 Jun 2017 - 31 Jul 2017)"
showing this "="" when i place cursor on this code

Item Selection from combo-box using vba in Internet Explorer

The below values represent a combobox / combo-list on a website. I am trying to select item #3 from the drop down using VBA. I have tried several things, but I have had no luck selecting said item. I have tried looking for solutions on Google and the Stack but haven't found anything that seems to work.
<DIV id=ext-gen256 class="x-layer x-combo-list x-combo-list-small" style="FONT-SIZE: 10px; HEIGHT: 92px; WIDTH: 113px; POSITION: absolute; LEFT: 744px; Z-INDEX: 12007; TOP: 235px; VISIBILITY: visible">
<DIV id=ext-gen257 class=x-combo-list-inner style="HEIGHT: 90px; WIDTH: 111px">
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="0">Select</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="1">Item 1</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="2">Item 2</DIV>
<DIV class=""x-combo-list-item" _nodup="30829" viewIndex="3">Item 3</DIV>
<DIV class="x-combo-list-item" _nodup="30829" viewIndex="4">Item 4</DIV></DIV></DIV>
I have used several variatons of the below code to try and select the item in question (i.e item #3), but keep getting a runtime error
Dim inputE As MSHTML.HTMLHtmlElement
Set inputE = IE.document.getElementsByClassName("x-combo-list-item")
If inputE.innerText = "Item 3" Then
inputE.Select
End If
This doesn't seem to work. Any help is greatly appreciated. Thanks!

excel web query in vba

I have a worksheet that currently updates about 200 stocks using Yahoo Finance API and MSXML. I would like to also get some other info from other websites that don't have an api, for example "http://www.earningswhisper.com/stocks.asp?symbol=googl".
Also for example, if you look at some info from that web page below, you see there is a Release Date of 1/29/2015. There is also some text in between, currently "[not confirmed]", but at some point it will change to "[confirmed]" and both items of text are of interest.
For lack of better web skills, I currently have a single sheet with a QueryTable that is refreshed in a code synchronously. It works...eventually. I would rather work with the response in code, but I don't know how to do that. I don't need this particular info to be auto refreshed.
Questions
Is there a preferred VBA way work with the html response? Can you show a code snippet to illustrate?
Is it possible to convert the html to xml or json relatively easy? code snippet?
If QueryTable is in fact the good enough solution, would it be faster to create a sheet for each stock and refresh async, using events?
I know there is oodles of info on the web, but most of it seems dated and confusing. I am using Excel 2013.
I am able to get at the data I want using html and grabbing Table(6) as shown in the code below. I guess I could parse the InnerText but I suspect there is an easier way to grab the elements I need from that table.
Sub TestHtml()
Dim Resp, sText, FirstCode As String
Dim oHttp, oFile, oTable As Object
Dim lines As Variant
Set oHttp = CreateObject("Microsoft.XMLHttp")
oHttp.Open "GET", "http://www.earningswhisper.com/stocks.asp? symbol=googl", False
oHttp.send ("")
Resp = oHttp.responseText
Set oFile = CreateObject("htmlfile")
oFile.Write Resp
Set oTable = oFile.getElementsByTagName("Table")(6)
sText = oTable.innertext
MsgBox sText
End Sub
Here is a line from that table, and the full table below.
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'">
<td width="67%" align=left valign=middle> Release Date: <font color='#505050'><small>[not confirmed]</small></font>
</td>
<td width="33%" align=right valign=middle>1/29/2015 </td>
</tr>
What is the best way to drill down and get at the elements in the table, using VBA code?
<TABLE cellpadding=1 cellspacing=0 border=0 id=QEsts width="100%" bgcolor="#505050"><tr><td><TABLE cellpadding=2 cellspacing=0 border=0 width="100%" bgcolor="#FFFFFF" height='148'><tr><td valign=top>
<table cellpadding=0 cellspacing=0 border=0 width="100%" class='newsart'>
<tr><td colspan="2" bgcolor="#FFFFFF"><table width="100%" bgcolor="#FFFFFF" cellpadding=1 cellspacing=0 border=0><tr><td style="background-image: url('images/headbar2.gif'); background-color: #000000; BORDER-RIGHT: #000000 thin solid; BORDER-TOP: #000000 thin solid; FONT-WEIGHT: bold; FONT-SIZE: 12px; MARGIN: 2px; BORDER-LEFT: #000000 thin solid; COLOR: #e1b64b; BORDER-BOTTOM: #000000 thin solid; FONT-FAMILY: Arial;"> 4th Quarter Ending December 2014</td></tr></table></td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> <b>Earnings Whisper</b> <small>&#174</small>: </td><td width="33%" align=right valign=middle><b>$7.24</b> </td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> Consensus Estimate:</td><td width="33%" align=right valign=middle>$7.16 </td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> Surprise Expectation <small><sup>1</sup></small>: </td><td width="33%" align=right valign=middle> </td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> Release Date: <font color='#505050'><small>[not confirmed]</small></font></td><td width="33%" align=right valign=middle>1/29/2015 </td></tr>
<tr><td width="100%" align=right colspan="2" valign=middle>After Close </td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> Expected Time <small><sup>2</sup></small>: </td><td width="33%" align=right valign=middle>N/A </td></tr>
<tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=top> Conference Call: </td><td width="33%" align=right valign=top>4:30 PM ET <small><br> </small></td></tr>
</table>
</td></tr></TABLE></td></tr></TABLE>
</td>
You can interact with DOM since you created that object successfully, but you know in your case the easiest way to get the necessary value from the table above is parsing the InnerText as string, try the code below:
aTmp0 = Split(sText, "Release Date:")
If Ubound(aTmp0) = 1 Then
aTmp1 = Split(aTmp0(1), "Expected Time")
MsgBox aTmp1(0)
Else
MsgBox "Release Date not found"
End If

hrefs not being rendered as links with EJS

I have an ejs file that i call from node.js that seems to be doing the right thing, but not rendering the links properly.
here is the call from node.js:
res.render('response.ejs', {jsondata : isu});
here is the code on the response.ejs page
<div>
<% for(var i=0; i < jsondata.length; i++) { %>
<li>
<%= link_to(jsondata[i].time, "Entry/"+jsondata[i]._id) %>
<%= jsondata[i].location.place[0]%>
</li>
<% } %>
</div>
this is what it returns (displayed on the page):
<a href='Entry/5411f73ef0fd92861601775f' >Thu Sep 11 2014 12:25:50 GMT-0700 (PDT)</a> Woodlawn Ave N
<a href='Entry/541251980570a2ba17f02c89' >Thu Sep 11 2014 18:51:43 GMT-0700 (PDT)</a> Woodlawn Ave N
<a href='Entry/5412524d0570a2ba17f02c8b' >Thu Sep 11 2014 18:54:29 GMT-0700 (PDT)</a> Woodlawn Ave N
<a href='Entry/541253b50570a2ba17f02c8d' >Thu Sep 11 2014 19:00:32 GMT-0700 (PDT)</a> Bagley Ave N
<a href='Entry/541254c054041703194a957d' >Thu Sep 11 2014 19:04:48 GMT-0700 (PDT)</a> Russell Ave NW
<a href='Entry/541502b197ed2f8022f0b399' >Sat Sep 13 2014 19:51:29 GMT-0700 (PDT)</a> Woodlawn Ave N
but if i copy this output to a file and open it in the browser i do see the hrefs rendered as links so no idea what is going on here.
<%= %> escapes HTML, so you need to use <%- %> instead.

Resources