Python selenium没有点击

问题描述 投票:0回答:2

所以我在这里失去了理智。我想点击一个特定的按钮,但有一个我不明白的错误:

Traceback (most recent call last):
  File "C:\Users\leosc\PycharmProjects\ogameBot\guideline.py", line 22, in guideline
    if metalMineLVL() > (10):
  File "C:\Users\leosc\PycharmProjects\ogameBot\guideline.py", line 10, in <lambda>
    metalMineLVL = lambda: c.metalMine.checkLVL()
  File "C:\Users\leosc\PycharmProjects\ogameBot\classes.py", line 121, in checkLVL
    a = self.sparte()
  File "C:\Users\leosc\PycharmProjects\ogameBot\classes.py", line 54, in <lambda>
    (By.XPATH, ('//*[contains(text(),\'{}\')]').format(sparte))))
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 64, in __call__
    return _find_element(driver, self.locator)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 415, in _find_element
    raise e
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 411, in _find_element
    return driver.find_element(*by)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\leosc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=72.0.3626.121)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)

这里的代码:

self.sparte = lambda: wait.until(                   
    EC.presence_of_element_located(
    (By.XPATH, ('//*[contains(text(),\'{}\')]').format(sparte))))
self.LVL = lambda: int(
    wait.until(EC.presence_of_element_located((By.XPATH, ('//* 
    [@ref=\'' + str(ref) + '\']/span/span')))).text)

def checkLVL(self):

    time.sleep(1)
    self.sparte().click()
    time.sleep(1)
    return self.LVL()


    metalMineLVL = lambda: c.metalMine.checkLVL()
    roboFabLVL = lambda: c.roboFab.checkLVL()

    try:
        if metalMineLVL() > (10):
            if roboFabLVL() <(5):
                c.roboFab.build()
    except:
        pass

当我激活调试器时,我看到每个东西都被处理,直到它到达

self.sparte = lambda: wait.until(                   
    EC.presence_of_element_located(
    (By.XPATH, ('//*[contains(text(),\'{}\')]').format(sparte))))

然后它进入异常部分。但是我一直在程序的其余部分使用这行代码,通常它工作得很好。我做错了什么?提前致谢!!

python selenium click
2个回答
0
投票

我不知道你为什么会得到这个错误。我最好的猜测是,这是因为名称空间冲突,但我不确定。这是我用来点击内容的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def clickElement(driver, XP):
    WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, XP)))
    driver.find_element_by_xpath(XP).click()

例:

clickElement(driver, '/html/body/div[3]/div/a[1]')

另请查看PEP 498以获取字符串格式。


0
投票

我现在发现问题是该程序打开了4个额外的选项卡,然后关闭了初始选项卡。通常情况下,我做了:

newTabs = c.driver.window_handles
        for tab in newTabs:
            c.driver.switch_to.window(tab)

让Selenium在新标签上工作。然而,编写新代码时,我忘了这样做,所以Selenium认为它仍然在初始标签上运行,该标签已经关闭。非常感谢!!

© www.soinside.com 2019 - 2024. All rights reserved.