【问题标题】:How to make a Loop with Tabs in Selenium Python如何在 Selenium Python 中使用选项卡制作循环
【发布时间】:2021-07-17 18:43:15
【问题描述】:

我正在尝试做一个无限循环。这使得在三个选项卡中的搜索成为 xpath 中的元素,如果找到该元素,请单击,如果没有找到,则转到下一个选项卡。

在#Important 的一部分!找到元素并单击。我需要做循环,我仍然不明白如何正确地制作我在每个选项卡中检查的循环。 :(

# Librerías
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import sys

#Open Three URL

driver.execute_script('window.open("https://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=000");')

driver.switch_to.window(driver.window_handles[0])

driver.execute_script('window.open("http://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=500");')

driver.switch_to.window(driver.window_handles[1])

driver.execute_script('window.open("http://locahost/login.php", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=500, top=0, left=1000");')

driver.switch_to.window(driver.window_handles[2])

#Login in three tabs 

driver.switch_to.window(driver.window_handles[0])

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                      'button.align-right secondary slidedown-button'.replace(' ', '.'))))\
    .click()

inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

driver.switch_to.window(driver.window_handles[1])
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

time.sleep(1)

driver.switch_to.window(driver.window_handles[2])
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('Admin')

time.sleep(1)

inputElement = driver.find_element_by_id("password")
inputElement.send_keys('12345678')

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login"]/div[4]/button')))
element.click();

time.sleep(1)

#Important! Find element and click

driver.switch_to.window(driver.window_handles[0])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

driver.switch_to.window(driver.window_handles[1])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

driver.switch_to.window(driver.window_handles[2])

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="fragment-pane"]/div/div/div[1]/div[1]/a/div')))
element.click();

【问题讨论】:

    标签: python python-3.x selenium


    【解决方案1】:

    你可以参考这个-How to loop through multiple Chrome browser tabs with Selenium and Python?

    还可以包含 try,except 块,这样如果元素未找到或可单击,则执行不会停止。代码可能如下所示

        xpath = "//*[@id='fragment-pane']/div/div/div[1]/div[1]/a/div"
        count = len(driver.window_handles)
    
        for i in range(count):
            driver.switch_to.window(driver.window_handles[i])
            try:
                element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, xpath)))
                element.click();
            except:
                pass
    

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多