Search code examples
pythonseleniumwebdriverwaitwindow-handlesexpected-condition

Selenium window_handles does not work for javascript button


I'm working on a selenium project. I need to click on javascript button and save my new url in selenium window_handles. My code is as following

window_before = driver.window_handles[0]
driver.find_element_by_xpath("//*[@id]/div/div[3]/div/button").click()
window_after = driver.window_handles[1]

but I got the following error:

window_after = driver.window_handles[1]
IndexError: list index out of range

Solution

  • It is Basically loading data on the same window NOT new window.However after search each page data you find 5 profile to view I have done 1st pages for you with company details.Here is the code.

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium import webdriver
    import time
    driver = webdriver.Chrome()
    
    driver.get('https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='searchLocationInput']"))).send_keys('China')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='ng-scope']//span[text()='CHINA']"))).click()
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchBtn"))).click()
    buttons=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"button[action-type='viewProfileButton']")))
    
    for i in range(len(buttons)):
        buttons = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "button[action-type='viewProfileButton']")))
        driver.execute_script("arguments[0].click();",buttons[i])
    
        item=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.partnerAddressAlign"))).text
        print(item)
        time.sleep(2)
        WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a[action-type='clickBackToResult']"))).click()
        print("=======================================")
    

    Console Output:

    5A, TOWER A, PACIFIC CENTURY PLACE, 2A GONG TI BEI LU, CHAOYANG DISTRICT, BEIJING
    BEIJING, BEIJING 100027 CHINA
    8032 Kms away 
    Gold Global Gold Master Specialized
    Visit Partner Website
    Show Additional Locations
    =======================================
    UNIT 1808, 18TH FLOOR CHINA WORLD TOWER 2
    NO, 1 JIAN GUO MEN WAI AVENUE
    BEIJING, BEIJING 100738 CHINA
    8034 Kms away 
    Gold Global Gold
    Visit Partner Website | Call
    Show Additional Locations
    =======================================
    702 TOWER W3 ORIENTAL PLAZA
    BEIJING, BEIJING 100738 CHINA
    8032 Kms away 
    Gold Global Gold
    Call
    Show Additional Locations
    =======================================
    Dimension Data House
    Building 2,Waterfront Business Park
    Fleet Road
    Fleet, HAMPSHIRE GU51 3QT UNITED KINGDOM
    282 Kms away 
    Gold Global Gold Master Specialized Customer Experience Specialized
    Visit Partner Website | Call
    Show Additional Locations
    =======================================
    110 BUCKINGHAM AVENUE
    SLOUGH, BERKSHIRE SL1 4PF UNITED KINGDOM
    259 Kms away 
    Gold Global Gold Customer Experience Specialized
    Visit Partner Website | Call
    Show Additional Locations
    =======================================