VBA selenium scroll windows to element point - vba

In VBA I need to move in Selenium viewport to the element to be clickable. I tried code below ;
.Mouse.MoveTo(.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a"),13,13) 'error Expeted = sign
.Actions.MoveToElement (.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a")) 'ineffective screen does not scroll down. Note: Windos is set to Maximize
the code is added but does not work, I could not add to the comment section
.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a").Click
.Actions.MoveToElement(.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a")).Perform
.Actions.MoveToElement(.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a")).Perform
.Actions.MoveByOffset(300, 300).Perform
.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a").Click

To move Selenium's focus to the element you can use either of the following lines of code:
.Actions.MoveToElement(.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a")).Perform
or
.Actions.MoveToElement(.FindElementByXPath("//*[#id=""main-content""]/section[2]/ul/li[2]/div/a"), 13, 13).Perform

Related

VBA macro how to click on button on website on chrome, using Selenium

I am trying to program to click on one button on website on chrome. The name of the button is "Add to Cart".
Please see HTML of the website:
enter image description here
And this is the VBA code:
CD.FindElementByCss("div.addToCartButtons clearAfter > button blue right s-addToCart > span.Add to Cart").Click
How can I do this?
Tags are important, you can anticipate some of the events attached to certain element in DOM just by reading its tag.
In your case you can click on the button tag directly instead of clicking the tag span, since this last one rarely has a .click event attached to it.
If you can provide an url to test this website I might help you better. This are the possible approaches:
1) Advanced:
a. Spaces in class are change for dots
b. Requires to understand tag's meaning and relative position of elements in DOM
'Example
'Clicking first button in div using only tag ("button" is a tag)
CD.FindElementByCss("div.addToCartButtons.clearAfter > button").Click
2) Intermedium:
a. Use only first Class and ignore all others after first space
b. Requires to understand what an elemment.child is
'Example:
'Clicking first child of "div" (Everything inside "div" is a child)
CD.FindElementByCss("div.addToCartButtons > button:nth-child(1)").Click
3) Easiest:
a. Double quotes ["] inside querySelector are changed to single quote [']
b. Requires to use Copy JS path in DevTools (Ctrl + Shif + I)
'Example:
'Clicking with Javascript in your website through ExecuteScript
strTemp = "document.querySelector('div.addToCartButtons clearAfter > button blue right s-addToCart').click"
CD.ExecuteScript (strTemp)

Selenium Webscraping - Print element that is not visible on the site but its on Inspect

enter image description here
I'm trying to print this 91040 from the site but I can't seem to do it and I think the reason is that 91040 is not visible on the site but only on inspect.
element = driver.find_element(By.XPATH,'//div[#class="code-container"]//span[2]')
When I paste this XPATH in the find bar on Inspect it locates the item but when I'm trying to print the element nothing gets printed. I tried these options.
print(element)
print(element.text)
So I'm thinking that the problem is that 91040 is not an actual visible text.
For anyone having the same problem I found the answer
hidden_text = element.get_attribute("textContent")

selenium send key for xpath not working

I want make automation for this web site
in this web site 3 text box are here check image
1st text box x path is /html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]
here is my code
driver.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]")).sendKeys("rio salon");
when I run this code I got this error
Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
How can i fix it? I hope my xpath is correct.
The field has aria-hidden=true attribute, so you get ElementNotInteractableException. You need to click on the dropdown first to change the attribute to true
WebElement dropdown = driver.findElement(By.xpath("//*[#id='search-form']/div/div[1]/div/div[1]/span"));
dropdown.click();
WebElement textField = dropdown.findElement(By.xpath("./parent::div/following-sibling::input[contains(#class, 'ui-select-search')]"));
textField.sendKeys("rio salon");
You can click in an input field with a div or span tag, but you cannot type in the field. So, your XPath must be written with an input tag if you want to sendkeys or type in an input field. For example:
//input[contains(#placeholder,'Site')]

Tosca: How to scan Dropdown textbox which disapper upon opening xScan

I have a problem in scanning a drop-down menu which disappears upon opening the xScan. I need to get the module id of the dropdown menu to verify some test steps.
Do you have any solution with this if it is not really possible to get the module id of the dropdown menu?
Open developer tools in your browser of choice (F12), navigate to the console and input the following code:
var fulldoc='';
var scrollX=0;
var scrollY=0;
document.addEventListener("keydown",function(event){
if(event.key=='q' && event.altKey){
fulldoc=document.body.outerHTML;
scrollY=window.pageYOffset;
scrollX=window.pageXOffset;
}
if(event.key=='w' && event.altKey){
document.body.outerHTML=fulldoc;
document.body.scrollTo(scrollX,scrollY);
}
});
When the window looks the way you would want to scan, press 'Alt + Q', then press 'Alt + W'.
Now your window will freeze and then you can scan your page.
To steer the objects you need to refresh your browser.
You can resolve the issue with below 2 steps
1 - Add some text in textbox which will populate the dropdown below it .
2 - Use Send Keys Module to scroll down and select the value.
I had a similar issue where we had a popup that only appeared when clicking on a text box. The solution we received from the Tricentis trainer was as follows:
Part One
1. Open your application in Chrome
2. Right click the inspect
3. In the inspector window, on the Elements tab, navigate to your html element where it should be (you can do that by clicking on the element and check that you can see the html in the element)
4. Use the debugger to add a break point there, this should pause it and you should be able to see the elements you need to steer it.
5. Once you found the element, you will need the type of element (e.g. div, span, etc), and the class name
Part two
1. Rescan your module and select any element that matches the criteria of your element selected in Part One #5
2. Identify it by only it's class name property and tag
3. Save and close
4. Edit the element in the module view by changing the class name. This should help you steer it
Note: if the element class name is not unique, you might need to use Explicit name.
Good luck

How to select value from Google auto location using Selenium

How to automate this to select particular value even the dropdown list id cannot be inspected. Can anyone help me out on this?
Need to select U.S. 22 Imperial from the list
Please find the HTML snippet
I am unable to proceed more than this. Please help me out!
WebElement location = driver.findElement(By.id("selectbox-city-list2"));
location.sendKeys("us");
You could use sendKeys and send arrow down to select an option. Selecting one by one with the arrow down will highlight the value. You will be able to check the highlighted value using the CSS class of highlighting.
You can use ActionClass
using this you can move your cursor over a specific element based on coordinates and perform a click.
1.So taking the coordinates of that text box.
2.Enter the full value in the text box. ,
3.calculate a very near coordinate to that text box(so that it will be the suggestion) and perform a click.
element = xxxxxxx;
Actions builder = new Actions(driver);
builder.moveToElement(element, x_coord, y_coord).click().build.perform();