Home > Back-end >  selenium python how to close popu tab
selenium python how to close popu tab

Time:08-08

if I click any of menu on this enter image description here

I tied those code but didn't work. driver.find_element_by_css_selector('.styles__ActionWrapper-sc-v9lptc-1 svg') , driver.find_element_by_xpath("//div[@class='styles__ActionWrapper-sc-v9lptc-1 eekxlt']")

CodePudding user response:

As I see

driver.find_element_by_css_selector('.styles__ActionWrapper-sc-v9lptc-1 button')

Or

driver.find_element_by_xpath("//div[@class='styles__ActionWrapper-sc-v9lptc-1 eekxlt']/button")

Should work, but you have to add a delay to make these elements fully loaded before clicking them.
The best way is to use visibility_of_element_located expected condition explicit wait.
Like this:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".styles__ActionWrapper-sc-v9lptc-1 button"))).click()

CodePudding user response:

The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='styles__ActionWrapper'] button[class^='styles__StyledButtonRoot']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@class, 'styles__ActionWrapper')]//button[starts-with(@class, 'styles__StyledButtonRoot')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

CodePudding user response:

I'm sure the other two answers are fine (I didn't test them, but I suppose their authors did) but here is my take on the whole issue, difference being the way I select the item as seen from a US connection:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label = 'Close Combo #1']"))).click()

This assumes you selected Combo #1 from the menu. You can change that locator dynamically, depending on the menu selected.

CodePudding user response:

ChromeOptions options = new ChromeOptions()

options.setExperimentalOption("excludeSwitches",

 Arrays.asList("disable-popup-blocking"))

Try this code

  • Related