How to click on the element as per the html through VBScript and Selenium - selenium

I'm using VbScript with Selenium driver to connect to Chrome (store.steampowered.com/app/681530/NOISZ/) and want to click the "Next in Queue" button which is a div class, but nothing happens. I know I'm on the right track because the below lines does work as expected:
(Displays the text Next in queue)
msgbox(driver.FindElementsByXPath("//div[#class='next_in_queue_area']/div[#class='btn_next_in_queue btn_next_in_queue_trigger']").Item(1).text)
(Scrolls to the Next in queue button)
driver.FindElementsByXPath("//div[#class='next_in_queue_area']").Item(1).ScrollIntoView
(But trying these ones does nothing and also no error messages)
driver.FindElementsByXPath("//div[#class='queue_actions_ctn']").Item(1).Click
driver.FindElementsByClass("next_in_queue_content").Item(1).Click
Using Chrome's Inspect when highligtning this code the Next button is highlighted entirely:
<div class="btn_next_in_queue btn_next_in_queue_trigger" data-tooltip-text="Remove this product from your queue and continue to the next item.">
<div class="next_in_queue_content">
<span>Next in Queue<br>
<span class="queue_sub_text">(11 remaining )</span>
</span>
</div>
</div>
So it is just a question of finding the correct div or span to click

To click the button with text as Next in Queue you can use either of the following solution:
FindElement with XPath:
driver.FindElementByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Click
FindElements with XPath:
driver.FindElementsByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Item(1).Click
As an alternative you can use the ExecuteScript() method as follows:
Dim js As IJavaScriptExecutor = TryCast(driver, IJavaScriptExecutor)
js.ExecuteScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))
Or
Imports OpenQA.Selenium.Support.Extensions
driver.ExecuteJavaScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))

Related

How do I "get" an element from the HTML that is a "data-index" on Selenium IDE webdriver (Chrome)?

I want to do a click on somethink in a website, but the problm is that I get an error: Element <td style="">...</td> is not clickable at point (366, 304). Other element would receive the click: <div class="fixed-table-loading" but where I want to click, does not have a class, it only have's a data-index what can I use to "click" on it ?
You can use below css selector for finding that element,
tr[data-index='0']
However the error "Element <td style="">...</td> is not clickable at point (366, 304). Other element would receive the click: <div class="fixed-table-loading"" you are getting is not means that you doing false operation, it means that where selenium find your desired element to click, there is already a different element on that position and it will be getting that click.
So, first you need to focus on your element or if it is a member of a dropdown or opening list you need to first open that dropdown and then you should have click your element.

How to click on the center of the button using selenium java? Which position of the button is actually getting clicked when button is identified?

I have button in our website, and border areas of the button is not clickable. So, i need to make sure the button is getting clicked at the center. Is it possible via selenium?
I tried using coorinates, but it is not recommended for our scenario.
xpath used : //div/button[#id='clickme']
<div class="col-lg-10 col-sm-12 p-0 pt-4">
<button class="click mb-3 " tabindex="0" id="clickme">+ Click Here</button>
</div>
Java code used to click
WebElement button = driver.findElement(By.id("clickme"));
button.click();
I guess the click is happening sometimes[2 out of 10 times] on the border where it is not clickable(hand symbol not shown) . As a result report says the click action happened, but there is no event fired on the website.
To identify the element with text as Click Here you can use either of the following Locator Strategies:
cssSelector:
"button.click#clickme"
xpath:
"//button[contains(#class, 'click') and #id='clickme'][contains(., 'Click Here')]"

Simulate Click event on a DIV element

I cannot simulate a click event on a div element. The HTML code is this one:
<div id="export" data-export="true" data-timespan="">
<button class="btn btn-default ladda-button" type="button" data-style="expand-right" data-spinner-color="#8899a6">
<span class="Icon Icon--featherDownload"></span>
<span class="ladda-label">
Exportar datos
</span>
<span class="ladda-spinner"></span></button>
<div class="hidden error server Callout Callout--danger">
<p>Ocurrió un problema al exportar tus datos, inténtalo de nuevo más tarde.</p>
</div>
</div>
In the webpage, is just a button that downloads a file when clicked. There is no URL. But after checking the code, is the div element what triggers the click event. Look the image.
Click to see image
That "g.handle" triggers a js.file form the website.
My vba code works perfect for doing other stuff in the website, but I cannot accomplish this task.
I've tried all this options:
Set div = HTMLDoc.getElementById("export")
div.FireEvent ("onclick")
div.Click
HTMLDoc.all.Item
Call HTMLDoc.parentWindow.execScript("g.handle", "JavaScript")
Nothing works. I'm kind of blocked right now and I have no idea of how to proceed. Maybe this has no solution? I have almost no idea of HTML and I have 0 idea of Javascript. But it must have a solution, because if I do it manually, i click on that DIV, wait some seconds and download my file.
Thanks in advance. Any help is welcome.
You didn't specify if the highlighted portion was the html code for the button or not, but it appears from looking at it that it isn't.
You can grab the class name for your button. The name may or may not be unique, and it's difficult to know that without seeing the entire html code, but you can set the btn variable below to the first instance of that class name. If there are multiple class names in your html code, then you will need to use a For Each...Next statement to iterate through them (unless you know the instance number off hand).
So we will go with the first instance of your class, hence the reason for the (0) appended on the end.
Sub Test()
Dim ie As New InternetExplorer, HTMLDoc As HTMLDocument, btn As Object
ie.navigate Url 'whatever this may be
' Some code to wait for your page to fully load
Set HTMLDoc = ie.document
Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
btn.Click
End Sub

Selenium: Click on element in div container doesn't work

Fyi: I'm using the selenium package for R, the selection code is equal to javascript or python so I'm asking a general selenium question.
I have a container which I have to make visible by a click, this works.
Then I try to select an element inside this container, I think I find it correctly but the click on the element only makes the popup window disappear.
Example code:
<div class="dateRanges" style="top: 275.313px; display: block;">
<a class="top dateOption CUSTOM" id="id32" href="javascript:;">
Benutzerdefinierter Zeitraum
</a>
<a class="dateOption TODAY" id="id33" href="javascript:;">
Heute
</a>
...
</div>
Element I try to find is "top dateOption CUSTOM".
My different tries which all failed:
remDr$findElement(using = 'xpath','//a[contains(#class,"top")]')$clickElement()
remDr$findElement(value = '//a[#class = "top dateOption CUSTOM"]')$clickElement()
remDr$findElements(using = 'css selector','a[class="top dateOption CUSTOM"]')[[1]]$clickElement()
I don't get any error message, the click seems to happen as the popup disappears, but the effect of the button does not.
I tried to save the object, wait a few seconds and click afterwards with no different effect.
I also tried different approaches with "idc4" with the same outcome.
Would appreciate any help, thank you.

Selenium clicking a sub menu

I am Really stuck for the past two days here. I am trying to click sub menu and when I try to click sub menu I get an errors as like the following
Element not found for the sub menu.
I have tried below code
WebElement element = driver.findElement(By.id("x-menu-el-P46915788081933044__folderbrowser_PJL"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
HTML Code
<li id="x-menu-el-P46915788081933044__folderbrowser_PJL" class="x-menu-list-item">
<a id="P46915788081933044__folderbrowser_PJL" class="x-menu-item" href="javascript:var abc = doNothing()" unselectable="on" hidefocus="true">
<img id="ext-gen926" class="x-menu-item-icon " src="netmarkets/images/import.gif">
<span id="ext-gen927" class="x-menu-item-text">Upload Documents from Compressed File</span>
Instead of using the ID you should probably use the class name.
WebElement element = driver.findElement(By.ClassName("x-menu-list-item"));
or you could try using the css selector
WebElement element = driver.findElement(By.cssSelector("li[class='x-menu-list-item']"));
Since the above return multiple items you could just use to return the exact element that you need:
WebElement element = driver.findElement(By.linkText("Upload Documents from Compressed File"));
1st click on the menu and then try the following statement -
driver.findElement(By.xpath("//li[#class='x-menu-list-item']//span[contains(text(),'Upload Documents from Compressed')])).click();
or directly try this -
driver.findElement(By.xpath("//span[contains(text(),'Upload Documents from Compressed')])).click();
I guess mostly the error is due to the span name spaces in between words, if the above dont work, pls attach a screenshot or give some details of the html code, so we can try more options, all d best.