I try to click on the login button(Đăng nhập) to show up the login box, but fail to achieve it.
The loginbox just doesn't show up.
Selenium, webdriver are all latest version
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
// driver.FindElement(By.XPath("//a[#href='#loginform']//span")).Click();
// driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']//span"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_username")));
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_password")));
// var loginBox= wait.Until(ElementIsClickable(By.Id("loginform"))); >> fail
driver.Scripts().ExecuteScript("document.getElementById('navbar_username').style.display='inline';");
driver.Scripts().ExecuteScript("document.getElementById('navbar_password').style.display='inline';");
Console.ReadKey();
}
C# extension:
public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
There are 2 problems.
1- There's a webpage coming up prior to the actual forum page, when you are navigating to the site. Below is the image for that:
So you have to click on the button, that is highlighted above first. And, then after you will be able to navigate to the forum's page.
2- Your button is certainly getting clicked, but since the webpage has not properly loaded, the click action is not proceeding.
Hence, you need to wait for certain element that gets loaded when the page is loaded properly.
Below code will help you out:-
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); //Give the implicit wait time
driver.FindElement(By.XPath("//button[#id='btnSubmit1']")).Click();// Clicking on the button present in prior page of forum
//Waiting till the element that marks the page is loaded properly, is visible
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ExpectedConditions.ElementIsVisible(By.XPath("//*[#id='vtlai_topx']/a")));
driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
...
You can proceed with rest then.
Related
I'm new to Selenium and try to automatically open a website in Full Screen mode.
The website has a Login which is already working with Selenium.
After the login only one button has to be pressed.
Hereby an WebdriverTimoutException is thrown in the second last line.
The InnerException says NoSuchElementException.
But when I open the web console, I can see the button.
IWebDriver driver = new EdgeDriver(System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + "\\webdriver");
driver.Navigate().GoToUrl(#"http://examplehomepage.com");
driver.FindElement(By.Id("username")).SendKeys("abc");
driver.FindElement(By.Id("password")).SendKeys("password123");
driver.FindElement(By.TagName("button")).Click();
driver.Manage().Window.FullScreen();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.TagName("button")));
element.Click();
I tried it with:
Edge (85.0.564.44)
Chrome (85.0.4183.83)
Firefox(80.0.1).
You can use sleep to delay the interaction
You are using ElementToBeClickable. You should also use ElementToBeVisible and also check whether element is enabled as well from isEnabled function.
When I try to use the selenium click in firefox, it doesn't wait till the element load completes.
Has anybody faced a similar problem?
First you should define default timeout of Webdriver after initialization as following :
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
After that you can wait that web element before clicking it like following :
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By locator));
I want to send instagram shipment with mobile imitation. but I need to click on a button for the new shipment. I tried everything about button click but I did not manage it.
public static void HoverMouseOverClicks(IWebElement element)
{
Actions action = new Actions(insta);
action.MoveToElement(element);
IJavaScriptExecutor executor = (IJavaScriptExecutor)insta;
executor.ExecuteScript("arguments[0].scrollIntoView(true);arguments[0].click();", element);
}
code click-through code you've seen above
HoverMouseOverClicks(insta.FindElement(By.XPath("//*[#id='react-root']/section/nav[2]/div/div/div[2]/div/div/div[3]/span")));
I try this and I still can not get the result.
insta.FindElement(By.XPath("//*[#id='react-root']/section/nav[2]/div/div/div[2]/div/div/div[3]/span")).Click();
This code sometimes clicks and sometimes does not click.
The XPath path never changes.
Relevant HTML:
<div class="q02Nz _0TPg" role="menuitem"><span class="glyphsSpriteNew_post__outline__24__grey_9 Szr5J">Yeni Gönderi</span> <span class="glyphsSpriteNew_post__outline__24__grey_9 Szr5J">Yeni Gönderi</span></div>
what should I do?
As per the HTML you have shared to click on the element with text as Yeni Gönderi you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#role='menuitem']//span[contains(#class, 'glyphsSpriteNew_post__') and contains(.,'Yeni Gönderi')]"))).Click();
Unable to click and select the value from dynamic drop down. Please find the below piece of code -
public static void main(String[] args)
{
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
//WebDriver Driver = new ChromeDriver();
WebDriver Driver = new FirefoxDriver();
Driver.get("http://www.spicejet.com/");
Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS)
Driver.findElement(By.xpath(".//*[#id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
}
Also I notice that Eclipse keeps on running after opening the Spicejet.com and it does not click on any drop down. To stop the execution I need to click manually on the Terminate button else it will not stop and go on for long time (4-6 hrs I believe)
You can use following code to select any value, In this code, I selected Goa (GOI). For more information, it is not a dropdown. It is a web table.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.spicejet.com/");
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXTaction")).click();
driver.findElement(By.xpath("//div[#id='dropdownGroup1']/div/ul[2]/li[4]/a")).click();
When I open that website, it sometimes seems to wait for the user to choose mobile or desktop site. Is that why it's hanging?
If I click past that (either with Selenium code or manually):
x1path = "//a[#class='desktop-view-button']"
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
this code opens the menu:
x1path = '//*[#id="ctl00_mainContent_ddl_originStation1_CTXTaction"]'
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
But then you have to choose which dropdown element you want (I don't think your code does that.)
x1path = '//div[#id="dropdownGroup1"]/div/ul/li[6]/a'
WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
driver.find_element_by_xpath(x1path).click()
The ul/li[6] selects the 6th element in the first column (Belagavi).
I wrote a simple code to submit a sign up form with Selenium. Before submit, driver should come from home page to sign up page.
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
If I print firefox.Title, it shows me title of home page currectly
And in home page, there is a sign-up button. Sign up button link is bellow.
<a target="_blank" href="SignUp.jsp">Register Here</a>
To navigate to sign up page, I wrote a line:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
After this, driver shows me the sign up page in new window of firefox browser. To navigate driver to the sign up I wrote firefox.Navigate();
Now If I print firefox.Title, it shows me title of home page again.
Please help me to find out problem. Thanks in advance.
You pretty much grabbing the same title since the you never switched to newly opened window
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
And, after switching to new window you should get new title.
However, this window handles process is utterly confusing to me. Selenium .Net bindings provide PopupWindowFinder class to handle windows.
Gratitude to JimEvans for his nice works and this
Use
firefox.SwitchTo().Window(handle);
where handle is one of instances found in firefox.WindowHandles. This will switch between the different window instances. You can find more information in the docs for IWebDriver.SwitchTo().