Selenium window_handles 不适用于 javascript 按钮

Selenium window_handles does not work for javascript button

我正在做一个 selenium 项目。我需要单击 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

从你的代码试验来看,你为什么期待多个 window_handles 并且你已经使用了:

window_before = driver.window_handles[0]

假设您有一个活动的 window_handle 并继续前进,您需要调用 click() 创建另一个 window_handle,以收集您需要诱导的所有 window_handles WebDriverWait with expected_conditions as 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
    

You can find a detailed discussion in

它基本上是在同一个 window 上加载数据 不是新的 window.However 在搜索每个页面数据后,您会找到 5 个要查看的个人资料 我已经为您的公司完成了第 1 页 details.Here是代码。

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
=======================================