How to get sub div in the loop in Selenium? - vba

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.

Related

findelementsbycss in vba with selenium

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

VBA Selenium Date Selector in iframe

I am trying to select a date but currently struggling. This date selector is in a iframe. I have switched to iframe but no nothing works to select the date, I have trying to change the data in this value part but it still does not work. I used the CSS, Xpath, ID, Class but nothing works.
.
I have found that using the Xpath is work for all the other elements like selecting time and dropdown.
With obj
'open edge
.Start "edge"
End With
'declare URL
Dim url As String
url = "http://cogprodweb.co.za"
'nagivate to url
obj.Get (url)
With obj
'Wait for frame to load and then Select the iframe
Application.Wait (Now + TimeValue("0:00:02"))
.SwitchToFrame ("iC93CD3A10B5343779D27E431CE7AF966")
'Select Locations
.FindElementByXPath("/html/body/form[1]/table/tbody/tr[2]/td/div/div/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[2]/div/select/option[9]", 3000).click
'Select Date
.FindElementByXPath("/html/body/form[1]/table/tbody/tr[2]/td/div/div/table/tbody/tr[2]/td/table/tbody/tr[3]/td[1]/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[1]/input", 3000).SendKeys("2021-07-19").click
I through identifying the calendar (which is table) and them running from the cells until it equal to inputted value (yesterday). I am not sure what do you for that. The variables, I have been looking at has not been working out. Looking at this to.
Iterating Through Table Rows in Selenium (Python)
it looks like the id of the iframe is being regenerated and the locator you're using might be no good i.e:
.SwitchToFrame ("iC93CD3A10B5343779D27E431CE7AF966")
either use a solid locator (add an id to the iframe element in html) or use a different locator meanwhile.
you can debug this and try to grab the iframe element with the id that was generated and see if indeed it's the locator issue or not

How to navigate through links in differents levels selenium vba

Actually, im trying to navigate to a block that has 3 levels.
First level im clicking to open the tree
Second level is an category with Href
Third level sometimes has subcategory with href too
So im trying to navigate through second and third level if exists.
My code works fine at level 1, it clicks all the items from level 1, but for the second level is not working properly.
What can be wrong at second level and how I can do to reach the third one?
driver.Get "https://www.tecnoglobal.cl/tiendaonline/webapp/ofertas/"
driver.Window.Maximize
Set categories = driver.FindElementsByXPath(".//*[#id='acordeon-productos-subcat']")
driver.FindElementByXPath(".//*[#id='js-productos']").Click
For Each category In categories
Application.Wait Now + TimeValue("00:00:01")
category.FindElementByXPath("ancestor::div/a/span").Click
Set categorylinks = driver.FindElementsByXPath(".//*[#id='acordeon-productos-subcat']")
For Each categorylink In categorylinks
linkcategory = categorylink.FindElementByXPath("descendant::div/descendant::span").Text
Thanks in advance.
The links are already present on page load so you don't need to click on products or navigate through tree. Simply gather an initial collection of the final nodes using a css attribute = value selector based on the ng-if attribute value. From that webElements collection extract the href attributes into an array, loop that array and .Get to each one.
Saving the links into an array avoids stale elements exceptions/element not found bubbling up when you navigate away from source page.
There are two patterns specified with Or "," syntax so as to match on 2nd Subcategoria and 3rd Subsubcategorias links.
Dim targets As WebElements, links(), link As webElement, i As Long
Set targets = driver.FindElementsByCss("[ng-if='!Subcategoria.leaf'],[ng-if='!Subsubcategorias.leaf']")
ReDim links(1 To targets.Count)
For Each link In targets
i = i + 1
links(i) = link.Attribute("href")
Next
For i = LBound(links) To UBound(links)
driver.Get links(i)
'do something on page
Next

How to open href link in chrome to VBA?

Its my first time using VBA.
Apparently i need it to help me edit the available stocks from various products (gonna need to learn about looping) and that various products left me with different href stocks edit link
Is it possible to click on the href link and open it ?
heres the element
heres the vba script
Sub AutoFill()
Dim obj As New WebDriver
obj.Start "chrome", ""
obj.Get "https://siplah.blibli.com/login/merchant"
obj.FindElementById("txt-email").SendKeys (ThisWorkbook.Sheets("Pass").Range("E20").Value)
obj.FindElementById("txt-password").SendKeys (ThisWorkbook.Sheets("Pass").Range("F20").Value)
obj.FindElementById("btn-login").Click
obj.Wait (500)
obj.FindElementByClass("modal-footer").Click
obj.FindElementById("menu-product").Click
obj.FindElementById("txt-product-name").SendKeys (ThisWorkbook.Sheets("31 CV").Range("E4").Value)
obj.FindElementById("btn-search-product").Click
End Sub
Thank you before!
Combining an attribute = value css selector with ends with operator to target href by the end if its href value, along with adding in the element className should be sufficient
obj.FindElementByCss(".link[href$='Slip']").click
Read about css selectors here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

Is there a way to click on anchor link

Please help me as the anchor tag looks like the below
<a title ="excel" class="activelink"style="Text-Decoration: none; onclick="$find('ReportViewerControl').exportReport('Excelopenxml');" href ="javascript:void(0)" alt="Excel" _selected="true"> Excel</a>
This doesn't have any document id or class.. Any help would be highly appreciated.
I try to check your HTML code and found that it contains the class but it can be possible that many other elements on the page using the same class. If you try to access the link using that class then it can possible that you click the incorrect element on the page.
We can see that the link contains the ' Excel' text. We can try to loop through all the links on the page and try to match the innerHTML to find that specific link.
Example:
'Below is code to loop through anchor tags, find the specific link, and click it.
Set elems = IE.document.getElementsByTagName("a")
For Each elem In elems
If (elem.innerHTML) = " Excel" Then
elem.Click
Exit For
End If
Next elem
Output:
In a similar way, you can also match other attributes from the anchor tag that may also help to click the link.
Note: This code example is for clicking the specific link on a page. It may not help you to automate the file download.
Further, you can try to modify the code example as per your own requirements.