0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mahdi
  • 967
  • 4
  • 18
  • 34
  • i think the handle doesn't change if you go to the next page. am i right? – 0xM4x Sep 27 '19 at 10:27
  • @M3hdi yes right – Mahdi Sep 27 '19 at 10:32
  • so is it opening a new tap or new window? – 0xM4x Sep 27 '19 at 10:32
  • Yes I see new window in selenium automated browser – Mahdi Sep 27 '19 at 10:38
  • I have two suggestions: you can maybe use driver.switch_to.window("NameofWindow") if you know the name of the new window. or you can close the existing tab and then use handle[0] – 0xM4x Sep 27 '19 at 10:43
  • 1
    Try add some sleep seconds after `.click()` – frianH Sep 27 '19 at 11:51
  • @Mahdi : If you could elaborate your problem with an example.OP might help you to resolving your problem.Thanks – KunduK Sep 27 '19 at 14:07
  • I did that before, But it does not work – Mahdi Sep 27 '19 at 14:07
  • @KunduK the webpage that I'm working on is https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619 When I go to this page and search for something lets say enter "China" in location and hit search, some results will appear in the page. When you want to see each result profile (via view profile button), new windows will appear but the page url will not change, So I cannot get new page_source to parse it. – Mahdi Sep 27 '19 at 14:10
  • 1
    @Mahdi : The strange thing is that it is not opening new window when I click on view profile it navigated the same window and url doesn't gets change.Possibly location issue or something else I don't know. – KunduK Sep 27 '19 at 14:40
  • @KunduK when you click on the view profile, new page will be shown with more details like website. It is working but the page url does not change. – Mahdi Sep 27 '19 at 14:42
  • @Mahdi : But I can see the details on the same window not new new tab that is why I am saying possible location issue. – KunduK Sep 27 '19 at 14:57
  • @KunduK yes my mistake. It is in same window but I cannot get new page source when I press view profile button with selenium. What should I do at that time ? – Mahdi Sep 27 '19 at 15:04
  • @Mahdi : Added an answer see if this helps. – KunduK Sep 27 '19 at 15:47

2 Answers2

2

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
=======================================
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

From your code trials it's not super clear why you are expecting multiple window_handles and you have used:

window_before = driver.window_handles[0]

Presuming you have a single active window_handle and moving forward you need to invoke click() which creates another window_handle, to collect all the window_handles you need to induce WebDriverWait with expected_conditions as number_of_windows_to_be(2) and you can use the following solution:

  • Code Block:

    windows_before  = driver.current_window_handle
    driver.find_element_by_xpath("//*[@id]/div/div[3]/div/button").click()
    WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
    windows_after = driver.window_handles
    

You can find a detailed discussion in Open web in new tab Selenium + Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The webpage that I'm working on is: https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619 The problem is when I search for partners and press view profile in each partner section, new window will shown but I cannot store its address in order to come back main search result page. – Mahdi Sep 27 '19 at 12:41
  • @Mahdi You can come back at your disposal but that should be a separate question all together. – undetected Selenium Sep 27 '19 at 12:42
  • No it is what I'm looking for from beginning – Mahdi Sep 27 '19 at 13:43
  • @Mahdi Your question was related to the error `IndexError: list index out of range`, however did you get a chance to see the reference discussion? – undetected Selenium Sep 27 '19 at 14:12
  • Yes I see that link, but they are working with real urls which I don't have in here and I can just navigate to new windows via xpath. – Mahdi Sep 27 '19 at 14:15
  • @Mahdi I am not sure what you mean by **real urls**. In your usecase clicking on the element `driver.find_element_by_xpath("//*[@id]/div/div[3]/div/button")` opens the new tab where as in the referred article `driver.execute_script("window.open('https://www.yahoo.com')")` is used to open the new tab? Do you see any other difference? – undetected Selenium Sep 27 '19 at 14:19
  • As you see they used 'https://www.yahoo.com' but in my case, I just hit a javascript button with not change in page url. – Mahdi Sep 27 '19 at 14:23
  • @Mahdi Doesn't _hitting the javascript button_ open a new tab? – undetected Selenium Sep 27 '19 at 14:24
  • No, could you please visit the url that I provide in my first comment ? – Mahdi Sep 27 '19 at 14:28