Selenium: Finding page element in WordPress - c#

Ok, this should be easy, but I cannot seem to figure it out:
How do I identify and access the new post area (from the Dashboard) in WordPress, using selenium in VS/.Net?
I can access the title field easily by ID, like this:
Driver.Instance.FindElement(By.Id("title")).SendKeys("Sometitle");
But, looking at the page source, I cannot figure out how to access the post body.
In recent versions, I believe, there was an iframe, and it could be accessed like this:
Driver.Instance.SwitchTo().Frame("content_ifr");
Driver.Instance.SwitchTo().ActiveElement().SendKeys(body);
...but this doesn't work anymore, and looking at the source it seems that this has been changed.
So - does anyone know how to do this in recent versions of WordPress?
EDIT: It turns out that I was wrong; there IS indeed an iframe named "content_ifr". So the new question is: Why doesn't the above code work? It's supposed to switch the focus to the content frame, but it doesn't.

try
Driver.FindElement(By.XPath(""));
to find XPath in google chrome right click on element->Inspect element, then click on "Copy XPath".
Hope it helps!

Related

C# WPF Selenium3 with Geckofx newest Version

Hi what I like to do is:
Create in WPF xaml a Grid like that: <_Grid Name="gridWeb">
Open a GeckoFX45 Firefox Browser in this Grid (add the created Geckofx Window as Child to the grid)
Automate this exact Browser in my Grid with Selenium.
I have made a lot of researches on that Problem and I found some articles like https://nhabuiduc.wordpress.com/2014/09/18/geckofx-net-webbrowser-setup-and-features/ on how to solve my issue. With that article I had success to solve point 1 and 2 but with old Version of Geckofx.
I have tried out tons of things, but nothing which included all requirements for my Tool.
Does anyone know if this is even possible?
If yes, does anyone know on how to combine all those 3 requirements with an actual version of Geckofx 45?
Is there any particular reasony why you want to add this browser to Selenium IWebDriver? (like e.g. lots existing code written for IWebDriver?)
If not and you simply want to have an automated browser then you can do much more automation using GeckoFx API.
For example:
GeckoWebBrowser Browser => GetBrowserInstanceSomehow();
...
//get element reference
GeckoInputElement textBox =
this.Browser.Document.GetElementsByClassName("inputBox").FirstOrDefault() as GeckoInputElement;
//set value
textBox.Value = "Something";
GeckoHtmlElement btn = this.Browser.Document.GetElementById("submitButton") as GeckoHtmlElement;
//interact
btn.Click();
You can do virtually everything with it - execute scripts, send POST requests, override CSS, evaluate / change / remove nodes, navigate, handle navigation events etc.

WebDriver - element is not clickable Chrome

I have following problem. I run test on Firefox and Chrome. On Firefox test run correctly but on Chrome SauceLabs give a message:
unknown error: Element is not clickable at point (717, 657). Other
element would receive the click: <div class="col-md-9 col-sm-12"
style="margin-top:8px;">...</div> (Session info: chrome=36.0.1985.125)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
I choose element by unique css selector in both test in the same way:
driver.FindElement(By.CssSelector("button.btn-xs:nth-child(1)")).Click();
Any ideas what is wrong here?
I am assuming that you have the correct element you need, ie the XPath is correct.
Here are few ways out:
Try to Click on the parent element instead.
Try .Submit() instead of .Click()
Try to execute the JavaScript that will be executed on the OnClick event of the element you are trying to click.
I have used the 3rd way with success all the time.
Another one
Do a .SendKeys(Keys.Enter) on that element (or a Space key)
Since you've tagged the question as Google-Chrome too - I suppose that this is happening mostly with ChromeDriver. I had the same issues with one of my previous projects (Asp .Net MVC). I found that when some elements are not visible for this Driver if they are not in the screen_visible_area. Please note that they are loaded (HTML, CSS3, JS etc.) properly.
So after a lot of reading and testing, I found that my workaround is simply scroll to the WebElement - so it is in the visible part of the screen. Actually this issue was not for all elements and I didn't find better solution for it.
unknown error: Element is not clickable at point (..., ...)
Is not descriptive error for this case, because like you I also thought that is Selector-related.
Just to be full answer - I had the same problems with IEDriver too. My implementation was to use the Browser scroll down/up options and just "send the screen" where the problematic element is.
Simple JSExecutor code that you can use:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(110,350)", "");
or
jse.executeScript("scroll(0, 250);");
or
driver.executeScript("window.scrollBy(110,350)", "");
Other topic-related useful resources are here.
Update
When it comes to the .sendKeys() I also used the browser accessibility features. All you need to do is just count how many TAB clicks your test need in order to get to the targeted web_element. Then just call .click().
Try this simple code:
element.sendKeys(Keys.TAB);
or
element.sendKeys("\t")
or
Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()
I realize this is a super old question, but it came up while searching a nearly identical problem in the present day. After attempting many of the fixes described here and getting new exceptions for my trouble (mostly stale element and http request timeouts) I stumbled across this issue on Selenium's GitHub.
As described in the post, Chrome had advanced beyond the abilities of my version of chromedriver.exe--my v2.30 driver had known issues with clicking elements due to changes in Chrome v61 scrolling mechanics. Updating to the latest chromedriver.exe solved all my problems.
tl/dr: ensure your version of chromedriver is compatible with the version of Chrome being tested.
I was getting issue that login button is not clickable in chrome even xpath was correct. after browsing many sites, i came to the solution - use .submit() instead of .click() and it worked perfectly.
driver.findElement(By.xpath("//button[#id='loginBtn']")).click();
driver.findElement(By.xpath("//button[#id='loginBtn']")).submit();
If you're doing anything complicated in your CSS, Chrome can get confused (this has happened to me) and think that the element you're trying to click is covered by another element even though this is not the case.
One way to resolve this is to help Chrome understand the situation correctly by adding z-indexes (you'll need to add relative or absolute positioning also) to unambiguously place the correct element on top of the other.
For me, it was creating this command instead.
driver.FindElementByXPath("id('gender1')").SendKeys(Keys.Space);
For my case, I was interacting with radio control.
I have had this problem on FF. This issue happens when your field is not in the view area. The slick way to resolve this issue is to zoom out your browser:
TheNotClickableField.SendKeys(Keys.Control + "-" + "-");
you might want to zoom out more or less according to your page size.
I.
If the button is on the bottom of the page, the following code could be used to get you to the bottom via JavaScript from where the click can be executed:
(driver as IJavaScriptExecutor).ExecuteJavaScript("window.scrollTo(0,document.body.scrollHeight - 150)");
The same action could be done via C# with the Actions class provided by Selenium.
This will get you to the bottom of the page----->
new Actions(Driver).SendKeys(Keys.End).Perform();
Keys.End could be switched with Keys.PageDown // Keys.Space
II.
If you want to get to the exact position of the element you can:
1.Get the element's Y location---> var elementToClick = driver.findElement(By.{Anything}(""));
2.Execute the following JS---> (driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,{0})", elementToClickYLocation.Location.Y));
3.Click the element---> elementToClick.click();

Try to get for WatiN for links/browsers

I've recently started to test and use WatiN, and i came across some of problems/errors that i cant really solve.
First ill describe what i'm doing with my tests ..
I'll go and get all the links in a specific div in a list ... so i have a link lists ... which im gonna loop thru ...
In the loop im gonna use a link.clicknowait() function, to open that link (it opens in a new ie tab) .. then i sleep thread for like few seconds and i close that browser by attaching that link (url) to a new browser and after that the browser is closed as im using it always with 'using (..)' statement.
So the first problem is that when the browser gets all the links and start to click them one of the links might lead not the right page, meaning like it can lead to a page that is saying page doenst exist anymore, so after that i cant close that page anymore ... how to solve this and attack ie to that not existing page?
It hasnt got fixed url ... anything that you can recommend?
Is there a method something like "Try attach to ...." because else i get an error if it tries to attack a browser to a link which doesnt exist.
Also is there anyway to check if the link in a links list is still the right one, cuz i also had an exeption few times that it couldnt click on that link because it was empty in the page ...
Or can i check something like link.trytoclick() and if an error and just ignores that link and goes further ???
And the last question after clicking alot of links and opening and closing browser i got an error outofmemoryexception .. how can it be, i've used 'using statement' which always closed browser when it was out of it ...
Thanks in advance for the help.
You can use the href to open the link in the same window of your watin session, after you verify the link you can go back to original page.
Some thing like this:
string href = browser.Link("link_id").GetAttributeValue("href"); //or your link from the collection
browser.GoTo(href);
//Perform you check
browser.Back();
To check if the link exists use:
session.browser.Back();

Click Gmail Refresh button

I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.

webBrowser control cannot find htmlElement after Ajax webpage update or in frame

Using webBrowser control in a winForm. but when the webpage is updated by Ajax or in a frame, I cannot use
webBrowser1.document.getElementById, etc. to find that htmlElement. The element also won't show in the View->Source code in IE.
The untimate purpose is to find that htmlElement and simulate a click or other function like
invokeMember("staff").
The WebBrowser's Document object does indeed represent a live view of the DOM so there may be some other reason that you're unable to find it. DOM updates will not however be represented in View -> Source. You should use IE8's developer tools which will show you a live view of the DOM and maybe you'll see something like an incorrect/duplicate ID or something.
I'm guessing you have already solved this problem on your own, but if you haven't, refer to my question here: WebBrowser Control and GetElement by ID
Essentially, if you do something to the WebBrowser control (ie, add some member to the DOM) it will do so asynchronously. That is, it does it on another thread, that way it avoids locking your calling thread when the WebBrowser is doing work. The problem is that if you programatically modify something with a command, you will have to wait till that command actually finishes loading its changes till you can work with the result of it.
Check my question there for a code example of what I was doing. I hope someone can find my previous trials useful.

Categories

Resources