Trouble with Pop up

I have an inconsistent pop up that I am trying to dismiss within my Synthetic Script. Ultimately, I would like to look to see if the popup is present, then either dismiss it, or move on with the script if its not present

.then(function(){
  log(6,'Check for My Offers Popup');
  var offer = $browser.isElementPresent($driver.by.className("pop-close"));
  if(offer === true) {
    console.log("Item located");
    $browser.findElement($driver.by.xpath("pop-close")).click();
    $browser.switchTo().defaultContent();
  }
  else {
    console.log("No Offer Popup");}})

Here is the info when I inspect the pop up

<a class="pop-close" href="javascript:void(0);"></a>

Hi @Clint.Durham try changing the $driver.by.xpath to $driver.by.className. I have found it best to always use the same method to find elements to eliminate confusion.

.then(function(){
  log(6,'Check for My Offers Popup');
  var offer = $browser.isElementPresent($driver.by.className("pop-close"));
  if(offer === true) {
    console.log("Item located");
    $browser.findElement($driver.by.className("pop-close")).click();
    $browser.switchTo().defaultContent();
  }
  else {
    console.log("No Offer Popup");}})

Thanks Stefan - I made that edit and i still get this error

TypeError: Cannot read property ‘className’ of undefined

Of course. The script is not getting the offer set due to the promise chain being broken. I haven’t put this in a script editor so there may be some closing parentheses missing.

.then(function(){
  log(6,'Check for My Offers Popup');
  return $browser.isElementPresent($driver.by.className("pop-close"))
  .then(function(offer){
	  if(offer === true) {
		console.log("Item located");
		return $browser.findElement($driver.by.className("pop-close"))
		.then(function(el){
			el.click();
		});
		$browser.switchTo().defaultContent();
	  }
	  else {
		console.log("No Offer Popup");}})
	});

So I couldn’t get the switchto element to work in the script and had to comment it out, everything else works

.then(function(){
log(6,‘Check for My Offers Popup’);
return $browser.isElementPresent($driver.by.className(“pop-close”))
.then(function(offer){
if(offer === true) {
console.log(“Item located”);
return $browser.findElement($driver.by.className(“pop-close”))
.then(function(el){ el.click(); });
// $browser.switchTo().defaultContent();
}
else {
console.log(“No Offer Popup”);}});
})

My problem now is that I am able to find and click the pop up but i can’t get back to the main page or default content

Sorry, you have to be careful where you place your execution and I haven’t tested this so I’m working from memory. This should work for you.

.then(function(){
  log(6,'Check for My Offers Popup');
  return $browser.isElementPresent($driver.by.className("pop-close"))
  .then(function(offer){
	  if(offer === true) {
		console.log("Item located");
		return $browser.findElement($driver.by.className("pop-close"))
		.then(function(el){
			el.click();
			$browser.switchTo().defaultContent();
		});
	  }
	  else {
		console.log("No Offer Popup");}})
	});
1 Like

@stefan_garnham - Thanks for the assist in this one but I think what’s going on here is the capital B is missing on the $driver.By. Using $driver.by instead of $driver.By will result in errors about the className or any other locators (css, xpath, etc…) throwing an error like @Clint.Durham is reporting. Thanks again :smile:

Thanks everyone! Here’s what finally ended up working for my use case.

// Step 6 POP UP CHECK POINT
.then(function(){
log(6,‘Check for My Offers Popup’);
return $browser.isElementPresent($driver.By.className(“pop-bg-overlay”))
.then(function(offer){
if(offer === true) {
console.log(“Item located”);
return $browser.findElement($driver.By.className(“pop-close”))
.then(function(el){ el.click(); })
.then(function(){
return $browser.sleep(5000);})
;}
else {
console.log(“No Offer Popup”);}});
})

1 Like

Awesome! Thanks for the update @Clint.Durham! :blush:

Glad you managed to get the result you wanted Clint.

Happy scripting!

1 Like