Im doing webtest using selenium Webdriver in C#. But I'm having a problem where when the browser window isn't in full size a popup will open half way outside the visible area.
The problem is that when i fire a .Click(); it doesn't do anything because the link i attempt to click is outside of the viewed area.
So how do i focus on the link to get click to work? Im currently using the following workaround but i don't think that's a nice way.
_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
_blogPostPage.FindElement(By.XPath(_popupLogin)).Click();
The sendkeys with space focuses on the link and makes Click work everytime, but isn't there a right way to do it?
We've been playing with Selenium and have run into this problem as well. I don't know if it's the WebDriver as a whole, the C# implementation, the version of Firefox etc, but we have found an ok workaround:
The trick is to force Selenium to evaluate the LocationOnScreenOnceScrolledIntoView property on the RemoteWebElement class (which is inherited by FirefoxWebElement and implements IWebElement). This forces the browser to scroll so that the element is in view.
The way we've done it is to use an extension method:
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace Namespace
{
public static class ExtensionMethods
{
public static IWebElement FindElementOnPage(this IWebDriver webDriver, By by)
{
RemoteWebElement element = (RemoteWebElement)webDriver.FindElement(by);
var hack = element.LocationOnScreenOnceScrolledIntoView;
return element;
}
}
}
this way all we have to do is change the generated code from:
driver.FindElement(By.Id("elementId")).Click();
to:
driver.FindElementOnPage(By.Id("elementId")).Click();
Hope it works for you!?
Instead of doing send key for blank value, send it for space. Thats the keyboard shortcut to select a checkbox.
Just replace the code :
_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
_blogPostPage.FindElement(By.XPath(_popupLogin)).Click();
by
_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys(Keys.Space);
driver.find_element(:id, "edit-section").send_keys " " with the space worked for me.
I am using webdriver rspec with selenium-server-2.24.1 and I was having trouble with IE8 - I kept getting Selenium::WebDriver::Error::ElementNotVisibleError. It was working in IE9 and FF but not IE8 until I added send_keys " ".
Related
The web page https://www.priceline.com/?tab=cars&vrid=7fb0c3635c8e8e7633afe152907a052e has an <input> element. When I click on it and start typing a <div> with a list of items below appears and I can choose from that list. But when I insert text into that <input> element i.e. the location field, where you see "CIty, Airport or Address" placeholder element on the webpage opened by Selenium, either by actually typing myself or via driver.FindElement(...).SendKeys(...), I see the text, but the list below is not showing.
I don't even know how to approach this. Do I need to configure the driver in a special way?
I assume there is some javascript that intercepts the typing and shows the list below. But, what can be the difference between typing in real life and through Selenium? What can I do?
Selenium Version
Selenium.WebDriver - 4.8.0
Selenium.WebDriver.ChromeDriver - 110.0.5481.7700
It is possible, the way you are doing it is not simulating typing into the input so the event listener that is listening for that simulation isn't being fired. Try this code below which simulates someone actually typing.
using OpenQA.Selenium.Interactions;
// Find the input element
IWebElement inputElement = driver.FindElement(By.Id("input-element-id"));
// Create an Actions object and send keys to the input element
Actions actions = new Actions(driver);
actions.MoveToElement(inputElement)
.Click()
.SendKeys("your text here")
.Perform();
The site is detecting Selenium use and blocking use of the site.
If you navigate to the site in a normal browser, typing in the search field works fine.
If you navigate to the site using Selenium and then use the site manually, the dropdown functionality still doesn't work. If you refresh the page, you get sent to a reCATPCHA page with the message, "Access to this page has been denied because we believe you are using automation tools to browse the website."
The desired element is a dynamic element, so to send some text e.g. Boston within City, Airport or Address field and click on the matching option you need to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#data-testid='startLocation-typeahead-input']"))).SendKeys("Boston");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#data-testid='typeahead-dropdown-card']//div[#role='option' and contains(., 'Boston')]"))).Click();
Browser Snapshot:
I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.
First question but I really am in a jam.
I have a webpage render which is working perfectly. However, I need to be able to control the initial display position (almost like a href #anchors in HTML) but without any access to the site content.
From as far as i can see i have no access to the scrollBars other than the bool to enable / disable..
Is there anything i can do to even force a scroll down of 20% for example, and then I can create a form to adjust later on.
Any assistance would be HUGELY appreciated although from what I have researched it seems unlikely.
I have the regular windows WebBrowser Render
private System.Windows.Forms.WebBrowser m_webBrowser;
Thanks !
--This is for c# standalone application.. Not WebBased.
Have you tried using jquery?
I personally use the animate method from jquery to scroll to certain elemnts in my webpage.
Example:
$('html, body').animate({scrollTop: $('#the-element-you-want-to-scroll-to).offset().top}, 1000);
PS: For the last parameter you can control the time it will use to scroll to destination, that offering you a nice effect.(in milliseconds)
I managed to resolve it using a strange method..
I basically injected some javascript into the rendered HTML manually.. Then the rest was easy.
i used something like this :
string updatedSource = WebBrowser.DocumentText.Replace("Google", "Foogle");
string extraSource =
"<html><body>Script goes here <br/>" +
"<div><p>BLA BLA BLA</p></div></body></html>";
WebBrowser.DocumentText = extraSource + updatedSource;
WebBrowser.Update();
Maybe it will help someone.
I am not able to Click on SubMenu item using selenium webdriver using c#.
I am using IE9 and FireFox 13.
I have tried Action Builder but it does not work.
It gives an error saying element cannot be clicked.
WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
Actions builder = new Actions(webDriver);
builder.MoveToElement(menu).Build().Perform();
Thread.Sleep(5);
//then click when menu option is visible
menuOption.Click();
I have used even javascript :
js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();
Please give some solution to click on hidden elements
You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).
The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.
Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.
After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which #Slanec is talking about in the above post.
By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.
I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -
new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver ) {
return driver.findElement(By.Id("ID2")).isDisplayed();
}
});
I hope that this helps you.
Scripts:
IWebDriver ie = new InternetExplorerDriver();
IWebDriver ff = new FirefoxDriver();
string baseURL = "http://xxxxxxxxxxxx";
ISelenium iesele = new WebDriverBackedSelenium(ie, baseURL);
ISelenium ffsele = new WebDriverBackedSelenium(ff, baseURL);
The page of baseURL has 2 frames and the upper frame is a warning page, and I want to select "Agree" then Click on "OK" to close it. Scripts of IE doesn't work, can discover the object of the checkbox and button, but "Select" and "Click" doesn't work. But under Firefox, it works, the upper frame was closed successfully.
Script:
IE
iesele.Start();
iesele.Open(baseURL);
iesele.SelectFrame("UpperFrame");
iesele.FindElement(By.Name("agree")).Click();
iesele.FindElement(By.CssSelector("ok")).Click();
Firefox
ffsele.Start();
ffsele.Open(baseURL);
ffsele.SelectFrame("UpperFrame");
ffsele.FindElement(By.Name("agree")).Click();
ffsele.FindElement(By.CssSelector("ok")).Click();
Does anyone know why IE cannot execute this script correctly?
Should I set something of IE8 ?
thanks
Hmm i have searched around for your issue; maybe you also have the same issue as in this stackoverflow question:
Selenium 2.0b3 IE WebDriver, Click not firing
Here it seems the given frame needs to have focus (via a click()) before the actual click() is registered on your element.
There are differences in the way each browser renders a page and also differences in the Selenium drivers (so it may not be possible to use exactly the same script for different browsers).
You might find this answer to a similar question useful. In particular, try selecting and clicking on a parent of the target element (such as a <div>) instead of the element itself. And also, try using MouseDown() followed by MouseUp() instead of Click().
I have found a few times that Firefox will work on finding and interacting with an element but IE will fail at various spots. Most of the time I throw in a 'wait for element' and the IE issue is resolved. I think IE is sometimes a bit slower creating elements (or does things in a different order?) so at times the element doesn't exist when you aim to click it.
This might not be your issue, but it seems to happen to me rather frequently!
edit: I also use Chrome, and often FF and Chrome work when IE fails.