How to select an element of a pop up prompt using Selenium - selenium

I'm writing a code using Selenium and Chromedriver to remotely control my instagram.
I've managed to login to the platform however as soon as I do a pop up asking about activating notifications appears and my codeflow breaks because it can no longer click the elements of the instagram page.
this is the code I tried using (which works during login to click the "send" button
notifBtn= self.browser.find_element_by_css_selector('button')
notifBtn.click()
(since it's the first instance of button on the web code it should automatically select that one)
however this time the browser is stuck and doesn't do anything.
during the login I had a problem where it wouldn't click either and found that it was because there was a lag between the request to go to the instagram login page and loading the page so it could find the CSS elements. I fixed it by adding
time.sleep(2)
before inputting the data and it fixed. I thought something similar would work here as it doesn't load instantly but it made no difference.
Is this an issue with the selector, as in could I use xpath to get around it? anyways sorry if it's a bit vague and feel free to ask me about my code or whatever.
notifBtn= self.browser.find_element_by_css_selector('button')
notifBtn.click()
I expect Chromedriver to click don't activate in the pop up so the code can continue instead I get stuck on that screen

Your find_element_by_css_selector call is probably locating multiple elements, and doesn't know which one to click. I would use an XPath here, to be more explicit about which button you are trying to click:
from selenium.webdriver.support import expected_conditions as EC
# Wait for button to exist
not_now_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[text()='Not Now']")))
# Click the button
not_now_button.click()

Related

Selenium Webdriver - how to find css/xpath for 'inline warning message' that disappears after few seconds?

I was trying to capture or (find the css/xpath) the inline warning message that disappears after few seconds (BTW, I am using Selenium WebDriver / Java for my automation).
eg: In the below public link, I try to click Reset Button without entering any email. The text box briefly shows 'Please fill out this field." I want to automate if it is showing this message as expected.
https://app.shipt.com/password_resets/new
Please help.
PS: I tried to search this website and google but could not find any useful information.
For actions that appear or disappear after certain time you should use Expected Conditions:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
And then you can click on element as usual.
However in case of the shipit page you are trying to automate the popup is a native HTML5 popup, so you cannot use Selenium directly to get the message, and you have to use this workaround:
Stackoverflow - how to get HTML5 error message in Selenium

How to sendKeys in textBox? for automation

I'm doing a test on c# with selenium for this site http://onliner.by.
At first I have to be authorized on this site.
I found(by xpath) button with name"Вход" in the right upper corner and click on it.
Then there's refreshed and page changed, but the link remained the same (http://onliner.by).
And I need to enter login and password on this page and sumbit it. But I cannot do it.
I founded Xpath paths of this elements and I used this code:
//this doesn't work
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[1]/input")).SendKeys("user");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[1]/div[2]/input")).SendKeys("password");
driver.FindElement(By.XPath("//*[#id='auth-container__forms']/div/div[2]/form/div[3]/div/button")).Click();
How can I do it? I tried to use SwitchTo().Frame but it didn't help too.
I will be very grateful for help.
You have to wait element availablity like this
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((Locator Value)));

cannot login with selenium

I am trying to automate logging into this website.
the login menu is at the top right of the website.
i have tried to find the login form by CSS selector, Xpath, name and the action of this topic.
edit 1 : I have also tried hover the mouse over the login with actionchains then find by elements. No Element was found, no key was sent also.
Result:
the find by CSS, Xpath, name shows : element not visible ;
the action event : i hover the mouse over the login area, send keys,the login area pops up but no key was sent, no error shown.
My code for the action event:
wd.get("http://www.vatgia.com")
action = ActionChains(wd);
action.move_to_element(wd.find_element_by_css_selector("#header_bar > div > div.fr > a:nth-child(8)"))
action.send_keys("loginsampling")
action.perform();
p/s : thankyou Brian for the edit !
Final update :
Mahipal has provided me with the solution. i should have used the xpath with regards to the container for the login form and password. i previously only right click and copy the xpath element and that did not work. Thanks Mahipal !
You can make use of the container of login menu for identifying its fields, as shown below:
driver.findElement(By.xpath("//div[#id='header_bar']//a[#rel='#header_login']")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(
driver.findElement(By.xpath("//div[#id='simple_tip']//div/input[#name='loginname']"))));
driver.findElement(By.xpath("//div[#id='simple_tip']//div/input[#name='loginname']")).sendKeys("login_name");
Try above code and let me know, whether it works for you.
This is due to the fact that you are not opening the login dropdown prior to attempting to find the login button. You will need to find the Dropdown element, click on it, then find the login fields and button and perform your actions.

Selenium WebDriver ElementNotVisibleError with IE 10 on Browserstack

I have rspec tests using Capybara which work great locally and on browserstack with a configuration of OS X Mavericks/Chrome 33 on browserstack.
When I change the configuration to Windows 7 / IE 10 I'm getting an ElementNotVisibleError on the last line of code represented here:
find('#myIdToExpandMyList').click
#click selected one
find(:xpath, "//SomeXPATHToRepresentAValueInMyList", :visible => :all).click
What is happening (I can see due to screenshots) is that the first line of code is not working. For some reason the click on this element is not working.
Here is an image of the expand (+)
When the user clicks on the plus sign the items in the list appear. Since the click isn't working the items never appear and the last line of code above doesn't work. Why doesn't this find/click work in IE 10 (with Selenium Webdriver)?
Here is the html code behind the expand:
<a id="myIdToExpandMyList" href="javascript:SomeJavscriptCallToExpandWithValues(params)">
<img src="plussign.png" alt="Expand">
</a>
UPDATE: In looking at this further this appears to be related to modal dialogs. In my case I have a modal dialog opening (z-index is set and the rest of the page is not reachable). For some reason (only in IE) I can't click on a link on the modal dialog using a capybara find(element).click. It seems to find the element otherwise I believe I would get an error on that.
Second UPDATE: After trying all sorts of things (falling back to selenium, different IE versions, native clicks, nothing worked. The only thing that worked was executing the javascript via execute_script. The plus sign (href) triggers a javascript function which opens the list - I called it directly. I do not like this solution so hopefully someone has a better one.
I am replying on behalf of BrowserStack.
I understand for your tests on IE 10, the logs show that the expand(+) button was clicked successfully. However, the click did not initiate the action (expand menu) it was supposed to. Thus, the subsequent actions failed.
As you have mentioned, you are able to run tests locally on your machine. Could you drop us an email with following details:
IEDriver version you use locally
Exact version of the IE browser you test on
Selenium Jar version

How to handle popup window using selenium webdriver with Java

Please help, I'm new in Selenium. I try to automate eCommerce website and I have problem to handle popup window. Here is the scenario:
Go to http://www.lampsplus.com
Click on Sale link in the header section.
Click on the 1st item/product displayed on the page. (This will show the product page).
On the product page, click on the red Add To Cart button. (This will add a product to cart and display a popup).
On the popup, click the dark-grey Continue Shopping button. (This will close the popup.)
I stuck on step 5 (Error message: Unable to locate element "Continue shopping button")
Here is my code before step 5:
WebDriver d1 = new FirefoxDriver();
d1.manage().window().maximize();
d1.get("http://www.lampsplus.com");
d1.findElement(By.name("hdr_sale")).click();
d1.findElement(By.xpath(".//*[#id='sortResultContainer60238']/a[2]/span")).click();
d1.findElement(By.id("pdAddToCart")).click(); //This is step 4
//Here is suppose to be some code which handles the popup - my problem
d1.findElement(By.id("aContinueShopping")).click(); //This is step 5
I'm aware about .getWindowHandle(); method. I tried several variations of it and none of them worked.
Can anyone give me an idea how to handle it. Many thanks! I use Java.
Note: I don't work for LampsPlus and not try to promote their products, this website was chosen for training purposes only.
The element aContinueShopping is contained within an iframe.
You'll have to switch to the iframe using:
WebElement frameID = d1.findElement(By.Css("#add-to-cart>iframe"));
d1.SwitchTo().Frame(frameID);
d1.findElement(By.id("aContinueShopping")).click();
There's no 'name' or 'id' on the iframe, so you'll have to use a WebElement or a numeric to find it.
Once you're done with that iframe, you'll switch back to 'top' by using:
d1.SwitchTo().DefaultContent();