Read an Element not present on Screen - C# Selenium - c#

I try to "read and click" an Element that is not present on the Screen.
My current quick & dirty solution is to zoom the body to 90%, than all works fine in this case. But this cannot be the right solution.
IJavaScriptExecutor js = (IJavaScriptExecutor)webdriver;
js.ExecuteScript(string.Format("document.body.style.zoom='{0}%'", 90));
The Webside i am working with is: https://eatradingacademy.com/software/forex-historical-data/
Scroll a little down, push "Load Data" and in the table down of this Button i am reading something out and download Files.
I searched around and found the "Actions". There came no error up, but also nothing happens ... The XPath is correct.
var element11 = webdriver.FindElement(By.XPath("//*[#id='table-acquisition']/ tr[7]/td[2]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(element11);
actions.Perform();
//element11.Click();
Also I found some Code to scroll a little bid down, again no error, but also no effect (no scrolling).
IJavaScriptExecutor js = (IJavaScriptExecutor)webdriver;
js.ExecuteScript("javascript:window.scrollBy(250,350)");
What am i doing wrong in this both code examples?
Or what is the solution to scroll to an Element that is currently not present shown on the Screen?
The complete Table is only readable if it is present on the Screen.
Who can help me?
Edit 1:
For more details in my question, here is c+p together was will be done in my code:
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = false;
//// change Standart-Download-Ordner
//ChromeOptions options = new ChromeOptions();
//var downloadDirectory = GlobalVars.RootPath + GlobalVars.strSymbol;
//options.AddUserProfilePreference("download.default_directory", downloadDirectory);
//options.AddUserProfilePreference("download.prompt_for_download", false);
//options.AddUserProfilePreference("disable-popup-blocking", "true");
//// Selenium Driver starten:
//webdriver = new ChromeDriver(service, options);
webdriver = new ChromeDriver(service);
// Fenster auf Vollbild, damit keine Elemente verdecht werden
webdriver.Manage().Window.Maximize();
webdriver.Navigate().GoToUrl("https://eatradingacademy.com/software/forex-historical-data/");
// deactivate Request --NOT YET-- "Would you like to receive notifications on latest updates?"
webdriver.FindElement(By.Id("webpushr-deny-button")).Click();
//// Current solution: Zoom Browser Body
//IJavaScriptExecutor js = (IJavaScriptExecutor)webdriver;
//js.ExecuteScript(string.Format("document.body.style.zoom='{0}%'", 90));
// change to right iFrame
webdriver.SwitchTo().Frame(webdriver.FindElement(By.Id("data-app-frame")));
// Navigate to "Settings" ...
var objBrowserLink = webdriver.FindElement(By.XPath("//a[#class='nav-link panel-switch' and contains(text(), 'Settings')]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(objBrowserLink);
actions.Perform();
objBrowserLink.Click();
// Change settings
webdriver.FindElement(By.Id("btn-reset-settings")).Click();
webdriver.FindElement(By.XPath("//select[#id='select-max-bars']/option[contains(text(), '200 000')]")).Click();
webdriver.FindElement(By.XPath("//select[#id='select-timezone']/option[contains(text(), '(GMT+02:00) Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius')]")).Click();
webdriver.FindElement(By.Id("input-server")).Clear();
webdriver.FindElement(By.Id("input-server")).SendKeys("AM Premium Data");
// Navigate to -Acquisition-
var objBrowserLink = webdriver.FindElement(By.XPath("//a[#class='nav-link panel-switch' and contains(text(), 'Acquisition')]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(objBrowserLink);
actions.Perform();
objBrowserLink.Click();
// Select Format to Download
webdriver.FindElement(By.XPath("//select[#id='select-format']/option[contains(text(), 'Expert Advisor Studio (JSON)')]")).Click();
// Click Button Load Data
webdriver.FindElement(By.Id("btn-load-data")).Click();
// --> Here starts my current problem:
// #####################################
// Test if switch will help again
//webdriver.SwitchTo().Frame(webdriver.FindElement(By.Id("data-app-frame")));
var element11 = webdriver.FindElement(By.XPath("//*[#id='table-acquisition']/ tr[7]/td[2]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(element11);
actions.Perform();
element11.Click();
//Thread.Sleep(1000);
// now follows a for ... The 2 lines down shoud do what is needed ...
//for (int i = 1; i < 8; i++)
string strXPath_Download_Table = "//*[#id='table-acquisition']/ tr[7]/td[6]";
webdriver.FindElement(By.XPath(strXPath_Download_Table)).Click();

Related

How to select Options-Button on specifig page C# Selenium

I try to select an option of a Button and found a working solution ;)
But i did not understand why the mostly posted standard will not work.
My solution is this line:
webdriver.FindElement(By.XPath("//select[#id='select-max-bars']/option[contains(text(), '200 000')]")).Click();
What i want to do:
Side: https://eatradingacademy.com/software/forex-historical-data/
Scroll a little down and Navigate to "Settings" (blue Text).
Here the Button for "Maximum bars" must be changed to 200.000.
Why will the Code just down not work for this?
Can anybody explain this for me?
Thank you
Here are the Side-Elements:
enter image description here
This is my not working code. The last 5 lines are the problem:
// Start driver
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = false; // hide Console
webdriver = new ChromeDriver(service);
webdriver.Manage().Window.Maximize();
webdriver.Navigate().GoToUrl("https://eatradingacademy.com/software/forex-historical-data/");
Thread.Sleep(1000);
// deny upcoming Request
webdriver.FindElement(By.Id("webpushr-deny-button")).Click();
// Navigate to Page (Link) = Settings
webdriver.SwitchTo().Frame(webdriver.FindElement(By.Id("data-app-frame")));
var statistics = webdriver.FindElement(By.XPath("//a[#class='nav-link panel-switch' and contains(text(), 'Settings')]"));
Actions actions = new Actions(webdriver);
actions.MoveToElement(statistics);
actions.Perform();
statistics.Click();
// Click Button "Reset"
webdriver.FindElement(By.Id("btn-reset-settings")).Click();
// for Test only: Button can be clicked
// //webdriver.FindElement(By.Id("select-max-bars")).Click();
// How to select the Option in the "Drop Down" Button "Maximum Bars" ?
// This here will not work for me:
// ###################################################################
SelectElement objSelect = new SelectElement(webdriver.FindElement(By.Id("select-max-bars")));
Thread.Sleep(15);
//objSelect.selectByText("200 000");
objSelect.SelectByValue("200000");

How to get a labels value in Selenium Chrome Driver using C#

I am making a bot that list all the given assembled letters to make the players connect or build a word around that given value of assembled letters, im very new to selenium and still grasping the best practices thanks in advance. (https://jklm.fun "BombParty")
https://i.stack.imgur.com/wGyMd.png
What we want to achieve:
//locate the element (the one the center of that bomb)
//extract the elements displayed value (the label)
//Console Log that value
My Code:
ChromeOptions options = new ChromeOptions(); //creating chrome driver options to add some commands to remove the message "This is automated . . ."
options.AddAdditionalCapability("useAutomationExtension", true);
options.AddExcludedArgument("enable-automation");
options.AddArgument("window-size=1200x600");
var driver = new ChromeDriver(options); //declear our driver for us to automate and control
driver.Navigate().GoToUrl("https://jklm.fun/QVVC"); // go to our targeted uri
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(25); // wait for sometime to give our client to load
// let our user enter the username or join a game
while (true) // loop our funtion to locate and extract the given word
{
try
{
driver.FindElements(By.CssSelector("div.syllable")); //we did a FindElements because the label can change value over time and state another syllable
Console.WriteLine("Success!");
}
catch
{
Console.WriteLine("Failed!");
}
}
The Output of my Code:
Success!
What I Have tried:
var word = driver.FindElement(By.CssSelector("div.syllable"));
Console.WriteLine(word.Text.ToString());
The Result: Failed!
Update:
So i tried to get the value after it finds it and i will find it again,it was a success but it only returns this string, how to fix this?
The update

Error using Selenium Webdriver headless

I'm using a chrome Webdriver to take a screenshot on a page, if I use the "headless" argument I get this error:
[1127/115120.051:INFO:CONSOLE(157)] "Deprecation warning: moment
construction falls back to js Date.
This is discouraged and will be removed in upcoming major release.
Please refer to https://github.com/moment/moment/issues/1407 for more info.
at Function.createFromInputFallback (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:157:187)
at ie (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:157:664)
at De (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:158:1390)
at Me (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:158:1287)
at Se (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:158:1007)
at Ne (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:158:1703)
at Oe (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:158:1737)
at t (https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js:154:2701)
at e (https://s-new.tradingview.com/static/bundles/blackfriday-dialog.dba1b059b906a510ba6c.js:1:580)
at i.<anonymous> (https://s-new.tradingview.com/static/bundles/blackfriday-dialog.dba1b059b906a510ba6c.js:1:2029)", source: https://s-new.tradingview.com/static/bundles/vendors.555f7dba06d6ca8b7950.4b2fc7ae22fe.js (157)
I'm not sure if the error and the info message are related.
The same thing happens using phantomJS, but works fine if the chrome window is shown, although that's not an option since every time the screenshot gets taken, chrome steals focus.
here's what I'm trying:
static void Main()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--disable-notifications");
options.AddArguments("--disable-gpu");
options.AddArgument("--headless");
var driver = new ChromeDriver(options);
driver.Url = "https://uk.tradingview.com/chart/?symbol=FX:GBPUSD";
var webElement = driver.FindElementByClassName("chart-widget");
string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".jpg";
Byte[] byteArray = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));
}
the error happens as the address is being passed to the driver.Url.
Any idea how to fix it or work around it?

chrome webdriver cannot open new tab

I'm trying to open up a new tab in Selenium.WebDriver.ChromeDriver" version="2.21.0.0" but it doesn't open anything, however if I move the debug tracking step back to the line "body.SendKeys(Keys.Control + 't')" to rerun the second time, it works ??
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement body = wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("body")));
Thread.Sleep(2000);
body.SendKeys(Keys.Control + 't');
Update: It seems to put a stop on chrome, it does open the tab properly. So instead of using Thread.sleep, just try:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("return window.stop");
body.SendKeys(Keys.Control + 't');
To open a new tab with Chrome:
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://stackoverflow.com");
// open a new tab and set the context
driver.ExecuteScript("window.open('_blank', 'tab2');");
driver.SwitchTo().Window("tab2");
driver.Navigate().GoToUrl("https://www.google.com");
Use the following code for your problem :
Actions act = new Actions(driver);
act.sendKeys(Keys.CONTROL,"t").build().perform();

Coded UI failing on the exception "Another control is blocking the control. Please make the blocked control visible ...." on a dialog window

I can't seem to get past this exception on my coded UI project for a window dialog box. I've tried using always search and some other options. I was trying to use the option of open a window dialog button but that didn't seem to work since i did not see the open button only "OK" and a few ones that didn't pertain to what i was trying to do.
I see one of my window is returning 5 windows, I'm trying to use the code Order Of overcation but that doesn't seem to work.
Currently here is my latest code which is still not working and still giving me the same exception error
WinWindow myDialogBox = new WinWindow();
myDialogBox.FilterProperties.Add(WinWindow.PropertyNames.OrderOfInvocation, "5");
myDialogBox.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogBox.SearchProperties[WinWindow.PropertyNames.ClassName] = "DirectUIHWND";
myDialogBox.SearchProperties[WinWindow.PropertyNames.ControlId] = "0";
myDialogBox.DrawHighlight();
WinWindow MyOpbenButton = new WinWindow(myDialogBox);
// myDialogBox.SearchProperties.Add(WinWindow.PropertyNames.OrderOfInvocation, "0");
MyOpbenButton.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
MyOpbenButton.SearchProperties[WinWindow.PropertyNames.Name] = "&Open";
MyOpbenButton.SearchProperties[WinWindow.PropertyNames.ClassName] = "Button";
//MyOpbenButton.SearchProperties[WinWindow.PropertyNames.ControlId] = "0";
MyOpbenButton.DrawHighlight();
//Testing this window produces 5 window with the same name and
control ID.
WinWindow matchingWindows = new WinWindow(myDialogBox);
matchingWindows.SearchProperties.Add("Name", "&Open");
// matchingWindows.SearchProperties.Add("ControlId", "0");
matchingWindows.SearchProperties.Add("ClassName", "Button");
UITestControlCollection windowsFound = matchingWindows.FindMatchingControls();
WinPane MyOpbenPane = new WinPane(MyOpbenButton);
MyOpbenPane.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
//MyOpbenPane.SearchProperties[WinPane.PropertyNames.Name] = "Open";
MyOpbenPane.SearchProperties[WinPane.PropertyNames.ClassName] = "Button";
// var myDialog = sharedControls.UIWindowsInternetExploWindow.UIWindowsInternetExplo_Open_Save_Dialog_LinksPage.UIItemWindow;
MyOpbenPane.DrawHighlight();
return MyOpbenPane;
The error message that this is producing is the following:
Another control is blocking the control. Please make the blocked
control visible and retry the action. Additional Details:
\r\nTechnologyName: 'MSAA'\r\nName: '&Open'\r\nClassName:
'Button'\r\nControlType: 'Window'\r\n
I used the test builder to get around this issue.
I found that the dialog screen was being captured with the
classname of 32770. I also found that i could not pass off my browser window to window from there i hand coded the remaining screen using the test builder.
here is the code below.
WinWindow window = new WinWindow();
window.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
window.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
window.SearchProperties[WinWindow.PropertyNames.ClassName] = "#32770";
window.SearchProperties[WinWindow.PropertyNames.Name] = "Windows Internet Explorer";
window.WindowTitles.Add("Windows Internet Explorer");
//window.DrawHighlight();
WinCustom myDialogBox = new WinCustom(window);
myDialogBox.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
myDialogBox.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogBox.SearchProperties[WinCustom.PropertyNames.ControlType] = "Dialog";
myDialogBox.SearchProperties[WinCustom.PropertyNames.Name] = "Windows Internet Explorer";
myDialogBox.WindowTitles.Add("Windows Internet Explorer");
//myDialogBox.DrawHighlight();
WinWindow myDialogwindow = new WinWindow(window);
myDialogwindow.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
myDialogwindow.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogwindow.SearchProperties[WinWindow.PropertyNames.AccessibleName] = "Windows Internet Explorer";
myDialogwindow.SearchProperties[WinWindow.PropertyNames.ClassName] = "DirectUIHWND";
myDialogwindow.WindowTitles.Add("Windows Internet Explorer");
return myDialogwindow;
I run into this a lot. I have an application on which the tabs always get reported as hidden, even though they're clearly visible. What has worked well for me so far has been to use keyboard.sendkeys to send keystrokes that select the tab.

Categories

Resources