-1

尝试打开登录页面并登录,此时以结尾的链接server_id=...将可用,这是一个用于编辑我的服务器列表的面板,然后我想单击底部的更新按钮。我使用By正确吗?

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

driver = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')

driver.get('https://minecraft-server-list.com/login/login.php')

WebDriverWait(driver, 3).until(
    expected_conditions.text_to_be_present_in_element(
        By.NAME, 'username') or
    expected_conditions.text_to_be_present_in_element(
        By.CLASS_NAME, 'column-heading'))

if expected_conditions.text_to_be_present_in_element(
        By.NAME, 'username'):

    user = driver.find_element_by_name('username')
    pw = driver.find_element_by_name('password')

    user.clear()
    user.send_keys('redacted')
    pw.clear()
    pw.send_keys('redacted')

    driver.find_element_by_name('Submit').click()

    WebDriverWait(driver, 3).until(
        expected_conditions.text_to_be_present_in_element(
            By.CLASS_NAME, 'column-heading'))

    driver.get('https://minecraft-server-list.com/login/edit_server.php?server_id=redacted')

    WebDriverWait(driver, 3).until(
        expected_conditions.text_to_be_present_in_element(
            By.CLASS_NAME, 'serverdatadiv1'))

    driver.find_element_by_name('button').click()

    driver.quit()

elif expected_conditions.text_to_be_present_in_element(
        By.CLASS_NAME, 'column-heading'):
    driver.get('https://minecraft-server-list.com/login/edit_server.php?server_id=redacted')

    WebDriverWait(driver, 3).until(
        expected_conditions.text_to_be_present_in_element(
            By.CLASS_NAME, 'serverdatadiv1'))

    driver.find_element_by_name('button').click()

    driver.quit()

driver.quit()

完整错误:

Traceback (most recent call last):
  File "C:/Users/veggie/Desktop/devshit/projects/mcslUpdater/main.py", line 14, in <module>
    By.CLASS_NAME, 'column-heading'))
  File "C:\Users\veggie\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "C:\Users\veggie\Anaconda3\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 209, in __call__
    element_text = _find_element(driver, self.locator).text
  File "C:\Users\veggie\Anaconda3\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 411, in _find_element
    return driver.find_element(*by)
TypeError: find_element() takes from 1 to 3 positional arguments but 5 were given
4

1 回答 1

0

似乎WebDriverWait中有错字。

根据定义,element_to_be_clickable()应该在 a 内调用,tuple因为它不是函数而是,其中初始化程序只需要隐式self之外的1 个参数:

class element_to_be_clickable(object):
    """ An Expectation for checking an element is visible and enabled such that you can click it."""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = visibility_of_element_located(self.locator)(driver)
        if element and element.is_enabled():
            return element
        else:
            return False

因此,有效地代替:

elif expected_conditions.text_to_be_present_in_element(
    By.CLASS_NAME, 'column-heading'):
    

你需要:

elif WebDriverWait(driver, 3).until((expected_conditions.text_to_be_present_in_element(
    By.CLASS_NAME, 'column-heading'))):

同样,您必须更改WebDriverWait的其他实例。


参考

您可以在以下位置找到详细讨论:

于 2020-07-08T06:01:06.067 回答