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

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

Related

How to select an element of a pop up prompt using 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()

Chrome driver unable to Identify Alert Message box

I am using Selenium Chrome Driver to test my application.
There is one page in the Browser where on Load an Alert message appears and I have to Click OK.
The problem is when I try to use driver.SwitchTo().Alert in my code. The Alert appears on the foreground page while the page is still loading. When I continue to debug at this point, my driver waits for an infinite time, and when I click manually on OK button, then it tries to switchto().alert, but since there is no Alert, it fails.
I would appreciate any help on this.
Note: The Page is in Loading Form till I click on Ok button on Alert , I wonder if it is alert
I also followed the below solution , but it does not work for me
https://groups.google.com/forum/#!topic/selenium-users/CixorzKZE4E
I get the following exception ,
he HTTP request to the remote WebDriver server for URL localhost:3200/session/0285afd8049f70878988405463448d24/… timed out after 60 seconds.
I can still see alert on my child window .
you could use explicit wait for the loading part.ie if webdriver instance is driver
WebDriverWait wait=new WebDriverWait(driver, //mention the time as per need here ie 20);
wait.until(ExpectedConditions.urlToBe("mention the url"));
before the alert handling code.
you need to accept or dismiss the alert, code you can try :
driver.switchTo().alert().accept();
I find out that the alert was a Javascript alert and how to handle javascript alerts is mentioned in Selenium Documentation.

how to automate "amazon-add-to-cart" procedure using java selenium without opening the browser

I want to automate "amazon-add-to-cart" procedure.
the flow is to send http get method of the requested items, and then
submitting a button of the returning html.
the 1st url for example is:
https://www.amazon.com/gp/aws/cart/add.html?AssociateTag=your-tag&ASIN.1=B003IXYJYO&Quantity.1=2&ASIN.2=B0002KR8J4&Quantity.2=1&ASIN.3=B0002ZP18E&Quantity.3=1&ASIN.4=B0002ZP3ZA&Quantity.4=2&ASIN.5=B004J2JG6O&Quantity.5=1%22
after that the user should click the continue button in order to indeed adding the items to the cart.
can you provide an example of how to code this flow with the button click using java selenium - without openning the browser?
thanks.
use the "HTMLUnitDriver" Webdriver
WebDriver driver = new HtmlUnitDriver();
driver.get("Enter the URL");

automation login to a website does not lead to the same page without automation (selenium)

I want to do some automation for a website using selenium and there is a login password to enter and when I automate the login it goes to one page, and when im not automating it goes to another(the right one)...the login info are identical and there is no explanation in the landing pages that will explain the behaviour...does anyone know if this means that the website added some security features that will detect automation?... I dont know even if its possible..will love to get help
this is what im doing:
driver.get("https://somewebsite")
val userNameTextFieldLocator: WebElement = driver.findElement(By.id("username"))
userNameTextFieldLocator.sendKeys("vvv")
val passwordTextFieldLocator: WebElement = driver.findElement(By.id("password"))
passwordTextFieldLocator.sendKeys("vvvv")
val pinNumLocator = driver.findElement(By.id("pinnumber"))
pinNumLocator.sendKeys("vvvv")
val logginButton: WebElement = driver.findElement(By.className("login_button"))
logginButton.click()
Then I see this security popup:
then to continue I go:
val alert: Alert = wait.until(ExpectedConditions.alertIsPresent())
alert.accept()
before initialize the driver try to:
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(cap);
Get the header Text from the Window Popup or the New page.
Next have a if-else case , if you get the above mentioned error--
Refer to these
Handle Securtiy Warning pop-up in FireFox with Selenium IDE
How to Click a option in Security Warning dialog box using Selenium?
If not, do as usual.

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();