1

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 3

1
+100

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()

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you, I tried your code with xpath. unfortunately it doesn't work. I tried debugging it and it shows an element not found exception at ("//a[@data-currency='EUR']"). By the way none of them, except ("//a[@id='switcher-info']") and maybe the save button, is clickable?
Normally I use Selenium with Java, but for other languages like Python and C# there is an ExpectedConditions too . VBA is missing this, and it's a problem... Can you put some hardcoded sleeps between all the clicks to let the dialogs open properly?
I tried that too, it responds with an "Element Not Interactable Exception". on ("//span[@class='select-item']"). When I remove .click() it responds with "Element Not found Exception" on ("//a[@data-currency='EUR']"). Have you tried it with Java? Does it work there?
("//span[@class='select-item']") still shows "element not interactable exception". Thank you so much for your effort!
it's all good you helped me out alot! thank you for your effort!
|
1

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()

3 Comments

Thank you! I tried, but it's not working unfortunately. (No Such Element Exception) at (".//div[contains(text(),'notifications')]//following::img")
Can you try without headless mode on to see what is happening on the page when page is complete?
I did and the problem was that it didn't click on "switcher-info". I had to hard-code a second click() for the code to work.
0

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()

1 Comment

Thank you, but it still shows an exception and says invalidselectorexception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.