vb.net selenium select element from dropdown - html

I don't know much about selenium. I'm trying to select an element from a dropdown to click on it, but vb.net doesn't find this element. or it finds another element that has the same class name.
It is the following dropdown menu on aliexpress.
And this is the code I have written so far....
Dim chromeDService = ChromeDriverService.CreateDefaultService() 'hides command prompt
chromeDService.HideCommandPromptWindow = True
Dim opt As New ChromeOptions()
opt.AddArguments("headless") 'prevents driver from opening a new window
Dim driver As IWebDriver = New ChromeDriver(chromeDService, opt)
driver.Navigate.GoToUrl(URL_AS_STRING) 'the URL as String
driver.Manage.Timeouts().ImplicitWait = TimeSpan.FromSeconds(2)
If driver.FindElement(By.ClassName("switcher-info")).FindElement(By.ClassName("currency")).Text = "USD" Then
Console.WriteLine("changing currency...")
'driver.FindElement(By.LinkText("EUR")).Click()
driver.FindElement(By.ClassName("switcher-info")).Click()
driver.Manage.Timeouts().ImplicitWait = TimeSpan.FromSeconds(1)
Dim element As IWebElement = driver.FindElement(By.ClassName("switcher-common")).FindElement(By.ClassName("switcher-currency-c")).FindElement(By.ClassName("select-item"))
Console.WriteLine(element.GetAttribute("innerText"))
End If
My question now is, how can I select and click on the element with the currency?
I intend to click on it so that it selects another currency.
Of course I could also make everything less complicated and include an exchange API. But the real rates do not match with the prices

You can do it simply by clicking the corresponding elements one by one exactly what the real user does via the GUI.
I see Selenium in VBA doesn't have explicit waits, so only the implicitly wait can be used as you already defined it driver.Manage.Timeouts().ImplicitWait = TimeSpan.FromSeconds(2) and since it was defined we do not need to define it anymore until we want to define it for some other value.
This is what I wrote.
I hope this should work:
If driver.findElementByXPath("//a[#id='switcher-info']//span[#class='currency']").Text = "USD" Then
Console.WriteLine("changing currency...")
driver.findElementByXPath("//span[#class='currency']").Click()
driver.findElementByCssSelector(".switcher-currency .select-item").Click()
driver.findElementByXPath("//a[#data-currency='EUR']").Click()
driver.findElementByXPath("//button[#data-role="save"]").Click()
In case there are pop-ups appearing on the aliexpress home screen disturbing the above code run try do the following:
If driver.FindElementsByXPath("//div[contains(text(),'Don')]").Count Then
driver.FindElementsByXPath("//div[contains(text(),'Don')]").Click()
If driver.FindElementsByXPath("//img[#class='btn-close']").Count Then
driver.FindElementsByXPath("//img[#class='btn-close']").Click()
If driver.FindElementsByXPath("//img[#class='close-btn']").Count Then
driver.FindElementsByXPath("//img[#class='close-btn']").Click()
And only after that run the code above so that the whole code will be something like this:
If driver.FindElementsByXPath("//div[contains(text(),'Don')]").Count Then
driver.FindElementsByXPath("//div[contains(text(),'Don')]").Click()
If driver.FindElementsByXPath("//img[#class='btn-close']").Count Then
driver.FindElementsByXPath("//img[#class='btn-close']").Click()
If driver.FindElementsByXPath("//img[#class='close-btn']").Count Then
driver.FindElementsByXPath("//img[#class='close-btn']").Click()
If driver.findElementByXPath("//a[#id='switcher-info']//span[#class='currency']").Text = "USD" Then
Console.WriteLine("changing currency...")
driver.findElementByXPath("//span[#class='currency']").Click()
driver.findElementByCssSelector(".switcher-currency .select-item").Click()
driver.findElementByXPath("//a[#data-currency='EUR']").Click()
driver.findElementByXPath("//button[#data-role="save"]").Click()

You need to close all the popups before interacting with any element on the page. Try this:-
driver.findElementByXPath(".//div[contains(text(),'notifications')]//following::img").Click()
driver.findElementByXPath(".//img[#class='btn-close']").Click()
driver.findElementByXPath(".//a[#id='switcher-info']").Click()
driver.findElementByXPath(".//div[#data-role='switch-currency']").Click()
driver.findElementByXPath(".//li/a[#data-currency='EUR']").Click()

I think, that the ClassName-Selector ist not the right one.
It is not a real dropdown like <select> but a dropdown-menue of links.
the Link for US-Dollar looks like this:
<span class="select-item chang-border" data-spm-anchor-id="a2g0o.home.1000001.i0.650c2145pgspp2">
<a rel="nofollow" href="javascript:;" data-spm-anchor-id="a2g0o.home.1000001.40">USD ( US Dollar )</a>
</span>
You can use the FindElementByCssSelector to access this link:
driver.FindElementByCssSelector("[data-spm-anchor-id=a2g0o.home.1000001.40]").Click()
For EURO it is:
driver.FindElementByCssSelector("[data-spm-anchor-id=a2g0o.home.1000001.43]").Click()

Related

Selenium not giving whats inside a class?

PATH = "D:\CDriver\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://www.se.com/ww/en/about-us/careers/job-details/inside-sales-associate/006ZMV')
TITLE = driver.find_element_by_class_name('sdl-application-job-details__job-title')
print(TITLE)
driver.quit()
I have all the needed imports, I just wanted to leave them out.
When I run this the output SHOULD be: Inside Sales Associate
But instead it gives me this: <selenium.webdriver.remote.webelement.WebElement, the session and element code.
What do I need to do to make it print what it should print. I have tried by_tag_name('h1.sdl-application-job-details__job-title') but that gives the exact same.
There is a inbuilt title method available in Selenium. You can call that method on driver object not on web element.
Code :
driver.get('https://www.se.com/ww/en/about-us/careers/job-details/inside-sales-associate/006ZMV')
driver.title
print(driver.title)
or if you want to retrieve text inside any web element, you could probably do something like this :
class_value = driver.find_element(By.CSS_SELECTOR, "h1[class$='sdl-application-job-details__job-title']").text
print(class_value)
The find_element methods return web elements. Just pass print(TITLE.text)

How can I change the href text using javascript im stuck

i got stuck 4 hours ago and i cant figure this out, how can i make the balance to go from 0.13 to $50.13?
Here is the code:
<li class="notranslate" id="Mata" translate="no">
Balance: $0.13</li>
Im trying to do this using the console in google chrome
If you set an id on the link like this:
<a id="link" href="/wallet">Balance: $0.13</a>
Then you can use:
document.getElementById("link").innerHTML = "Balance: $50.13";
If you are not able to set an id then you can do:
document.querySelector("link[href='/wallet']").innerHTML = "Balance: $50.13";
The chrome inspect tool also has a feature where if you click the icon in the top-left of the tool, it will let you select an element from the page. After selecting an element, you can refer to it in the console as $0. So you after selecting the link you could do:
$0.innerHTML = "Balance: $50.13";
Since you do not have an id assigned to your a tag, you can use document.querySelectorAll, which will return a list of elements that match the specified selector.
Then, you can access each element using its index number, which in your case is 0, since you have only one element.
To change the text, simply use the text property:
document.querySelectorAll("a[href='/wallet']")[0].text = "Balance: $50.13";

How to Execute js of a webelement in Selenium

On a target Webpage, I have a HTML Button elements with following sub-tags (Excess HTML statements removed for brevity)
<a class="ebookreader" onclick="getBook2Read('12','Journey of India','Author-JN')">Read</a>
<a class="ebookreader" onclick="getBook2Read('32','Journey of Punjab','Author-JN')">Read</a>
<a class="ebookreader" onclick="getBook2Read('33','Journey of Haryana','Author-JN')">Read</a>
<a class="ebookreader" onclick="getBook2Read('38','Journey of Uttar Pradesh','Author-JN')">Read</a>
In my VB 2019 Code using Selenium Driver, I want to execute the js code related with onclick.
I have tried following statements, but in vain
For Each book In driver.FindElementsByClassName("ebookreader")
book.GetAttribute("onclick")
book.GetCssValue("onclick")
Next
But am not able to move ahead. I even tried creating filling a ListBox but the values added in the ListBox are shown as OpenQA.Selenium.Remote.RemoteWebElement or OpenQA.Selenium.Chrome.ChromeWebElement
Is there a way to invoke onclick js from Selenium?
Got it
For Each book In driver.FindElementsByClassName("ebookreader")
Dim onClickValue As String = book.GetAttribute("onclick")
If onClickValue <> "" Then
listBooks.Items.Add(onClickValue)
End If
Next
listBooks is the VB.Net forms ListBox Control

How do I click a button on a webpage through VBA?

I am trying to use click a button through vba but it tells me that the "Object doesn't support this property or method".
My VBA code right now is:
IE.document.getElementByType("button").getelementByClass("qm_historyTab_GoButton").Click
The HTML for the button is:
<button class="qm_historyTab_GoButton" type="submit" style="font:normal 11px tahoma,arial,helvetica,sans serif;">
<span class="qm_historyTab_GoButtonText">GO</span>
</button>
Try this:
IE.document.getElementsByTagName("button") _
.getelementsByClassName("qm_historyTab_GoButton")(0).Click
Both of the getXXX methods return a collection of items (even if there's only one match in the document) so to address a single item from the last one in order to click it you need to add the (0)
EDIT: if the aim is to submit the form then this will also work
IE.document.getElementById("qm_historyForm_7871").submit
or as there's only one button with that classname:
IE.document.getElementsByClassName("qm_historyTab_GoButton")(0).Click
Ok so I found the solution in this specific instance. The button is a submit type and sends a message using HTMLInputElement.
What I needed to do was add "Microsoft HTML Object Library" to my references and utilize those features.
I had to create the variable that would send the response:
Dim htmlInput As MSHTML.HTMLInputElement
I couldn't figure out how to refer to this specific element individually so I grabbed everything with it's class name.
For Each htmlInput In IE.document.getElementsByClassName("qm_historyTab_GoButton")
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
Then I had the program cycle through each item in the series and send a submit response. Since this was the only one with the class name, I got lucky.
So anyways, that's how I solved the problem.

Clicking a button within an IE table

I am trying to click a button within a table on a webpage within IE, the source of the button shows:
<input type="image" src="img/testimg.png" onclick="picture_return(this,'92b84574a336a090618f151b6fc821cf:5','http://testwebpage.com/in/834');" value="Test Web Button">
This is a part of a large table with multiple <td> within the source, this is within another table which is then within the following class:
<div class="section_client_dnBox">
I tried to go through a few of the items within the class by using the following VBA code:
IE.Document.getElementsByClassName("section_client_dnBox")(0).Click
However, had no luck as (0) didn't press anything and anything larger ie, (1) gave me an error. So my question now is basically, is there any way of clicking the button using something simple such as reffering to it's value within the table (value="Test Web Button")?
From my experience, you need to look at the tag name rather than the class name. This is an example of the code I generally use when finding buttons.
For Each MyHTML_Element In document.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then
MyHTML_Element.Click: Exit For
End If
Next
You might be able to change the . type to = "image". I too am just learning how to use IE automation in VBA so I am not a champ at it either. I hope that helps.
CSS selector:
It is far simpler to use a CSS selector of input[value='Test Web Button']. No loop required.
It says get element with input tag having attribute value having value = 'Test Web Button'. "[]" means attribute.
.querySelector method of document is how you apply the selector.
CSS query:
VBA:
ie.document.querySelector("input[value='Test Web Button']").Click