vb.net selenium select element from dropdown

Viewed 411

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.

enter image description here

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

3 Answers

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