How can I select an option from a drop down list on Chrome using VBA? - html

' Sub chromeAuto()
Dim obj As New WebDriver
Dim doc As HTMLDocument
obj.Start "chrome", ""
obj.Get "https://www.website.com/" #######Opens Chrome
Application.Wait DateAdd("s", 3, Now)
obj.FindElementById("userid").SendKeys ThisWorkbook.Sheets("Sheet1").Range("A1").Value
Application.Wait DateAdd("s", 1, Now)
obj.FindElementByName("password").SendKeys ThisWorkbook.Sheets("Sheet1").Range("B1").Value
obj.FindElementById("btnActive").Click'
##Logs into website
Application.Wait DateAdd("s", 2, Now)
obj.FindElementById("pt1:_UIScmil2u::icon").Click
obj.FindElementById("pt1:_UIScmi4").Click
Application.Wait DateAdd("s", 4, Now)
obj.FindElementById("pt1:r1:0:r0:0:r1:0:AP1:soc2::drop").Click #######Opens Drop down list
Application.Wait DateAdd("s", 2, Now)'
# Below is the source code I get from the button I want to select
I am trying to select option 2
`<ul class="x1qz" id="pt1:r1:0:r0:0:r1:0:AP1:soc2::pop"><li id="pt1:r1:0:r0:0:r1:0:AP1:soc2::sp"
class="x1r4"></li>
<li class="x1r8 p_AFSelected" _adfiv="0">Option 0</li>
<li class="x1r8" _adfiv="1">Option 1</li>
<li class="x1r8" _adfiv="2">Option 2</li>
<li class="x1r8" _adfiv="3">Option 3</li>
<li class="x1r8" _adfiv="4">Option 4</li>
<li class="x1r8" _adfiv="5">Option 5</li>
<li class="x1r8" _adfiv="6">Option 6</li>
<li class="x1r8" _adfiv="7">Option 7</li>
</ul>
`

Difficult to know without seeing more html but try css attribute selector of
obj.findElementByCss("[_adfiv='2']").click

Related

Nodejs: Getting plain text from HTML but retaining ordered and unordered lists

I am attempting to obtain the plain text from a piece of HTML code but would like to retain the numberings of from the ordered or unordered list. So far, libraries such as node-html-parser and cheerio do not retain those information.
Meaning to say, given a HTML like so:
<ol>
<li>Number 1
<ol style="list-style-type: lower-alpha;">
<li>Number a</li>
<li>Number b</li>
</ol>
</li>
<li>Number 2
<ol style="list-style-type: lower-alpha;">
<li>Number a
<ol style="list-style-type: lower-roman;">
<li>Number i</li>
<li>Number ii</li>
</ol>
</li>
<li>Number b</li>
<li>Number c</li>
</ol>
</li>
</ol>
I would like to obtain:
1. Number 1
a. Number a
b. Number b
2. Number 2
a. Number a
i. Number i
ii. Number ii
b. Number b
c. Number c
I write in Nodejs.
import {
parse
} from 'node-html-parser';
var html = `<ol>
<li>Number 1
<ol style="list-style-type: lower-alpha;">
<li>Number a</li>
<li>Number b</li>
</ol>
</li>
<li>Number 2
<ol style="list-style-type: lower-alpha;">
<li>Number a
<ol style="list-style-type: lower-roman;">
<li>Number i</li>
<li>Number ii</li>
</ol>
</li>
<li>Number b</li>
<li>Number c</li>
</ol>
</li>
</ol>`
const root = parse(html);
var children = root.querySelector('ol')
var m = children.innerHTML
var lis = parse(m)
var m = lis.text
console.log(JSON.stringify(m))
output will be
 
Number 1 
 
Number a 
Number b 
 
 
Number 2 
 
Number a 
 
Number i 
Number ii 
 
 
Number b 
Number c 
 

CSS children selector (not being able to select all children)

This is the image of what I'm trying to scrape using beautiful soup. But whenever I use the code shown below, I only get access to the first child. I am never able to get access to all the children. Can someone help me with this?
item = soup.select("ul.items > li")
print(len(item))
The problem can be fixed in 2 steps as follows:
Use select_one on soup to get the ul
Use find_all on ul to fetch all the li items.
Working solution:
# File name: soup-demo.py
inputHTML = """
<ul class="items">
<li class="class1">item 1</li>
<li class="class1">item 3</li>
<li class="class1">item 3</li>
</ul>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(inputHTML, 'html.parser')
itemList = soup.select_one("ul", class_="items")
items = itemList.find_all("li")
print("Found ", len(items), " items")
for item in items:
print(item)
Output:
$ python3 soup-demo.py
Found 3 items
<li class="class1">item 1</li>
<li class="class1">item 3</li>
<li class="class1">item 3</li>
Maybe your version is wrong. This is OK.
from bs4 import BeautifulSoup
html = '''
<ul class="items">
<li>1</li>
<li>2</li>
</ul>
'''
soup = BeautifulSoup(html,features="lxml")
item = soup.select('ul.items>li')
print (len(item))
There's another solution here
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = '''
<ul class="items">
<li>1</li>
<li>2</li>
</ul>
'''
doc = SimplifiedDoc(html)
item = doc.selects('ul.items>li')
print(len(item))
Here are more examples here

Why is this asp repeater inserting an extra <li> (or <tr>) into every "ItemTemplate"

The asp.net repeater is inserting an extra <li></li> into every <ItemTemplate>. It does the same thing with tables. It inserts an extra <tr></tr>. And same with divs. Basically any element that I put in the ItemTemplate comes out with a duplicate.
Here is my code.
This is part of that builds the litData the aspx.vb file:
If item.ItemType = ListItemType.Item Then
Dim drv As System.Data.DataRowView = DirectCast((e.Item.DataItem), System.Data.DataRowView)
Dim strLinkValue As String = drv.Row("ReturnVal").ToString()
Dim Literal1 As Literal = DirectCast(item.FindControl("litData"), Literal)
Literal1.Text = "<a href=" & strQString.ToLower().Replace("/default.aspx", "") & strLinkValue & "/default.aspx>Hello World" + strLinkValue + "</a>"
End If
And this is in the .aspx file:
<asp:Repeater ID="rptData" runat="server" OnItemDataBound="rptData_OnItemDataBound" EnableViewState="false">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:Literal ID="litData" runat="server" EnableViewState="false"></asp:Literal></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
I am expecting it to render...
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
What is actually rendering...
<ul>
<li>Item 1</li>
<li></li>
<li>Item 2</li>
<li></li>
<li>Item 3</li>
<li></li>
</ul>
As per comment you are binding data to the repeater inside ItemDataBound handler. That is not the right way to do it. Move your data binding to the Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
// other code
if (!IsPostBack)
{
rptData.DataSource = objDS;
rptData.DataBind();
}
}
And you might not need the Item Bound handler anymore

Wordpress Custom Menu Walker

I am just starting with WP theming and I'm stuck with the walker class. I am trying to output the following code:
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
<li class="hasDropdown">
Menu 3 - Level 1
<div>
<div>
<h5>Description 1</h5>
<ul>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
</ul>
</div>
<div>
<h5>Description 2</h5>
<ul>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
</ul>
</div>
<div>
<h5>Description 3</h5>
<ul>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
</ul>
</div>
<div>
<h5>Description 4</h5>
<ul>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
<li>Menu 3 - Level 2</li>
</ul>
</div>
</div>
</li>
<li>Menu 4</li>
<li>Menu 5</li>
I have read numerous articles about the walker class but each does a small portion of what I am trying to do. I would like to bring it all together and get a better understanding. I am unsure how to start / end and setup the menu where it has a drop down level. Please can anyone help? Many thanks in advance.
I have tried copying and amending the walker class found in nav-menu-template as follows:
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<div>\n";
$output .= "\n$indent<h5></h5>\n";
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
$output .= "\n$indent</div>\n";
}
This gives me the:
<div><h5><ul>
I am struggling to understand how to achieve the wrapping div and the .hasDropdown
Try overriding one more method. (It is implemented in Walker class which Walker_Nav_Menu extends).
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
$id_field = $this->db_fields['id'];
if (! empty( $children_elements[$element->$id_field] )) {
$element->classes[] = "hasDropdown";
}
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
And for wrapping with div element, you have to dig deeper in display_element method (located in wp-includes/class-wp-walker.php), but I'm sure you will know what to do.

Passing the id of an HTML <li> tag to a VBScript subroutine when clicked on

I have loads of VBScripts that I plan to give a GUI front end (using an HTA). At the top of the HTA's window I have a drop down menu bar created as follows (haven't included CSS code as I don't think that it adds any relevent information).
I have been trying for sometime but have been unable to work out how I can pass the value of the id of the clicked li to the MenuClicked subroutine.
My previous experience with HTML was using it to create static documents so I might have got this all wrong. If that is the case then please let me know.
<script type="text/VBScript">
Sub MenuClicked()
Select Case WhatDoIPutHere
Case "#Option1A" : Sub_Option1A
Case "#Option1B" : Sub_Option1B
Case "#Option2A" : Sub_Option2A
Case "#Option2B" : Sub_Option2B
Case "#Option3" : Sub_Option3
End Select
End Sub
</script>
<div>
<ul id="nav" class="drop" onClick="MenuClicked()">
<li><a>Option1A</a>
<ul>
<li id="#Option1A"><a>Option 1A</a></li>
<li id="#Option1B"><a>Option 1B</a></li>
</ul>
</li>
<li><a>Option 2</a>
<ul>
<li id="#Option2A"><a>Option 2A</a></li>
<li id="#Option2B"><a>Option 2B</a></li>
</ul>
</li>
<li id="#Option3"><a>Option 3</a></li>
</ul>
</div>
Since HTAs are run by an Internet Explorer engine, you should use the srcElement property
<script type="text/VBScript">
Sub MenuClicked()
Dim target
' .parentNode because the <a> element is really what's being clicked,
' but we want the ID of the <li> element
Set target = window.event.srcElement.parentNode
Select Case target.id
Case "#Option1A" : Sub_Option1A
Case "#Option1B" : Sub_Option1B
Case "#Option2A" : Sub_Option2A
Case "#Option2B" : Sub_Option2B
Case "#Option3" : Sub_Option3
End Select
End Sub
</script>
<div>
<ul id="nav" class="drop" onClick="MenuClicked()">
<li><a>Option1A</a>
<ul>
<li id="#Option1A"><a>Option 1A</a></li>
<li id="#Option1B"><a>Option 1B</a></li>
</ul>
</li>
<li><a>Option 2</a>
<ul>
<li id="#Option2A"><a>Option 2A</a></li>
<li id="#Option2B"><a>Option 2B</a></li>
</ul>
</li>
<li id="#Option3"><a>Option 3</a></li>
</ul>
</div>
Here's how I would do it:
<script type="text/VBScript">
Sub MenuClicked(obj)
Select Case obj.id
Case "#Option1A" : Sub_Option1A
Case "#Option1B" : Sub_Option1B
Case "#Option2A" : Sub_Option2A
Case "#Option2B" : Sub_Option2B
Case "#Option3" : Sub_Option3
End Select
End Sub
</script>
<div>
<ul id="nav" class="drop">
<li><a>Option1A</a>
<ul>
<li id="#Option1A" onClick="MenuClicked Me"><a>Option 1A</a></li>
<li id="#Option1B" onClick="MenuClicked Me"><a>Option 1B</a></li>
</ul>
</li>
<li><a>Option 2</a>
<ul>
<li id="#Option2A" onClick="MenuClicked Me"><a>Option 2A</a></li>
<li id="#Option2B" onClick="MenuClicked Me"><a>Option 2B</a></li>
</ul>
</li>
<li id="#Option3" onClick="MenuClicked Me"><a>Option 3</a></li>
</ul>
</div>