0

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

2 回答 2

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
=======================================
于 2019-09-27T15:47:16.897 回答
1

从您的代码试验中,您并不清楚为什么您期望多个window_handles并且您使用过:

window_before = driver.window_handles[0]

假设您有一个活动window_handle并向前移动,您需要调用click()它创建另一个window_handle,以收集window_handles您需要的所有内容,以使用expected_conditions诱导WebDriverWait,您可以使用以下解决方案: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
    

您可以在新标签 Selenium + Python 中的 Open web 中找到详细讨论

于 2019-09-27T12:29:26.387 回答