findelementsbycss in vba with selenium - vba

i can't manage to extract certain links from a website with css.
i don't really know css yet either and am just finding my way around it.
i have 2 questions one specific how to find the element, and at the end again how to examine the element in css.
1.
Inspection of the site with markers
i am trying to get the two links marked in green, as marker i would like to use either the red marked link text or above it.
my attempted code:
Set Elements = GC.FindElementsByCss("table.TABLE tbody tr td p iframe ")
a = 0
For Each Element In Elements
ReDim Preserve href(a) As String
href(a) = Element.Attribute("innerText")
a = a + 1
Next Element
can i somehow examine the elements in vba?
if i know what is stored in them, i could search for the desired value under Value and then use the expression.
I think under Watches I once had the possibility to see all the different values for an element like innerText, href, outerhtml, innerhtml and so on.
In VBA Watches at the moment i only see a small overview

Related

How to get sub div in the loop in Selenium?

I am attempting to scrape the website
https://justjoin.it/
I want to get all texts inside every job offer.
To get links to job offers I am using Xpath:
Dim elems as Selenium.WebElements
Set elems = mw.FindElementsByXPath("//div[contains(#class,'css-ic7v2w')]/div/div/div/a)
For Each webele In elems
Debug.Print webele.Attribute("href")
Debug.Print webele.Text
Next webele
and it is working fine.
The issue is that I want to go deeper into each specific div (3 divs after) and get the "alt" attribute which is the job offer name.
How to make this work? I suppose it will be some kind of sibling element?
I tried to loop through each webelement and get the siblings of it but I failed.
So what i want to get is to find specific div and loop through next nested divs inside.

VBA macro how to click on button on website on chrome, using Selenium

I am trying to program to click on one button on website on chrome. The name of the button is "Add to Cart".
Please see HTML of the website:
enter image description here
And this is the VBA code:
CD.FindElementByCss("div.addToCartButtons clearAfter > button blue right s-addToCart > span.Add to Cart").Click
How can I do this?
Tags are important, you can anticipate some of the events attached to certain element in DOM just by reading its tag.
In your case you can click on the button tag directly instead of clicking the tag span, since this last one rarely has a .click event attached to it.
If you can provide an url to test this website I might help you better. This are the possible approaches:
1) Advanced:
a. Spaces in class are change for dots
b. Requires to understand tag's meaning and relative position of elements in DOM
'Example
'Clicking first button in div using only tag ("button" is a tag)
CD.FindElementByCss("div.addToCartButtons.clearAfter > button").Click
2) Intermedium:
a. Use only first Class and ignore all others after first space
b. Requires to understand what an elemment.child is
'Example:
'Clicking first child of "div" (Everything inside "div" is a child)
CD.FindElementByCss("div.addToCartButtons > button:nth-child(1)").Click
3) Easiest:
a. Double quotes ["] inside querySelector are changed to single quote [']
b. Requires to use Copy JS path in DevTools (Ctrl + Shif + I)
'Example:
'Clicking with Javascript in your website through ExecuteScript
strTemp = "document.querySelector('div.addToCartButtons clearAfter > button blue right s-addToCart').click"
CD.ExecuteScript (strTemp)

vb.net how to get value of <strong> using htmlagilitypack

How to get value under using hhtmlagilitypack. by the way there are many<tr><td> from this site, so would be much better if I can get the specific value under this "Social Security Number"
<tr><td><span>Social Security Number</span></td><td><strong>524-23-6748</strong></td></tr>
I tried using this code but no luck
Dim webGet As New HtmlWeb
Dim doc As HtmlDocument = webGet.Load("https://www.fakeaddressgenerator.com/World_Address/get_us_address/city/Chicago")
Dim work As HtmlNodeCollection = doc.DocumentNode.SelectNodes("/html/body/div[1]/div[3]/div[1]/div/div/div[2]/div[1]/div[2]/table/tbody/tr[6]/td[2]/strong")
TextBox1.Text = work.First().Attributes("value").Value
This one is different to the last; the xpath is right except for the tbody. It's important to remember that the document inspector you see in a browser might not reflect the actual source, and the source is what HAP parses
Easiest way I find to debug these is to remove the trailing part of the path and look what I get in the locals window:
In terms of how to know if it's attibute values or inner text etc, that comes from looking at the structure of the HTML:
Yellow highlights are attributes (things between < and > that is in the form name="value" is an attribute name/value pair), red underlines innertexts (anything between > and <)
All in your code is right apart from the TBODY, and that you're trying to select the Value of an attribute called "value" but your strong doesn't have any attributes, it only has an inner text

Is it possible to colour a cell with value from data in Cognos BI?

Let me first be clear. I'm not asking about how I do conditional formatting in Cognos BI. If there were a simple Red/Amber/Green colour scheme, based upon value ranges then I could do that. If it were a static list of colours, which never changed, I could also do that.
What I am after is accessing a hex colour code that is stored in my database, and I want to use that colour as my table cell background colour. This is something I commonly do in SSRS reports, but cannot see a method for in Cognos BI.
Is this even possible?
You can do this via the HTML object in Cognos.
The HTML object can get its definition from one of the three main ways:
1) Hard-coded text
2) Data Item Value
3) Report Expression
Obviously the first method provides no way to dynamically set the value. I couldn't get the second one to work at all. I'm not yet sure why. However, I was able to use the third type to work to allow dynamic setting of a visual style.
For the solution we'll assume you have a data item called [Color] which pulls a string value from a database in the standard hex form that is used in CSS: #xxxxxx, e.g. #CCCCCC. For the purpose of this example we'll assume it is in query Query1. The following steps describe how to set it up.
1) Add an HTML item right above your list
2) Add another HTML item at the bottom of your list
3) In the top HTML item add a span tag with a unique id such as:
<span id="list">
4) In the bottom HTML item add a closing span tag
</span>
5) Add a third HTML item before all of the other HTML items
6) Set the 'Source Type' property of the HTML item to 'Report Expression'
7) In the Report Expression put the following code:
'<style>
#list td {
background-color: ' + [Query1].[Color] + '
}
</style>'
8) Select the Page object and set the Query property to Query1
9) Click on the Properties property. Check the Color column to give the page access to that query-sourced value.
Now you can dynamically set the column color based on a database provided value. We used the span to give us a way to isolate just the table cells we want to manipulate.
The technique isn't perfect. For instance, the header cells also get their background changed to the color in question, which may or may not be desirable. This is because Cognos doesn't use the th tag for headers but instead renders them as normal cells (td).
I know it's quite and old post but just for completeness I'll add the references to get this working in html, pdf and excel.
To get this working not only for html but also for pdf and excel use a rich text item instead of a html item.
You can use following code in a query item for instance:
<span style="display:block; background-color:' + [Query Subject].[Query Item] + '"> </span>
The query item must then contain a valid color (e.g. rgb(255,0,0)) etc. which is defined by your data source.
Dragging a rich text item in a list and changing it to data item value and selecting the query item will work.
By using the span it will work for excel too, however to make sure it follows the size of the upper object in the hierarchy (the list column or a table etc) you want the display:block style.
Instead of the space in between the > < you can use any other query item that you want to appear as text.

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());