我正在从事硒项目。我需要单击javascript按钮并将新的URL保存在Selenium window_handles中。我的代码如下
window_before = driver.window_handles[0]
driver.find_element_by_xpath("//*[@id]/div/div[3]/div/button").click()
window_after = driver.window_handles[1]
但出现以下错误:
window_after = driver.window_handles[1]
IndexError: list index out of range
答案 0 :(得分:2)
基本上是在同一窗口中加载数据,而不是在新窗口中加载。但是,在搜索每个页面数据后,您会找到5个配置文件以进行查看,我已经为您完成了第一页的公司详细信息。这是代码。
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("=======================================")
控制台输出:
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
=======================================
答案 1 :(得分:1)
根据您的代码试用,并不清楚为什么要使用多个window_handles
并使用过:
window_before = driver.window_handles[0]
假设您有一个活动的window_handle
,然后向前移动,则需要调用click()
,它会创建另一个window_handle
,以收集诱导window_handles > WebDriverWait ,其中expected_conditions为number_of_windows_to_be(2)
,您可以使用以下解决方案:
代码块:
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
中找到详细的讨论