出现错误“传入的无效定位器值".如果我们使用find_element而不是find_element_by [英] There is error "Invalid locator values passed in" in case we use find_element instead of find_element_by

查看:214
本文介绍了出现错误“传入的无效定位器值".如果我们使用find_element而不是find_element_by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python-Webdriver自动执行点击"操作.这是我的代码:

I'm using Python-Webdriver to automate a "click" action. Here is my code:

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.common.exceptions import InvalidSelectorException


LOGIN_BUTTON = (By.XPATH, '//a[contains(@class,"aui-nav-link login-link")]')
NEWS_OPTION = (By.ID, 'blq-nav-news')

driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("http://bbc.co.uk/")
myDynamicElement = driver.find_element(NEWS_OPTION)
myDynamicElement.click()

控制台会生成如下异常

    raise InvalidSelectorException("Invalid locator values passed in")
selenium.common.exceptions.InvalidSelectorException: Message: Invalid locator values passed in

但是,如果我更改了行

"myDynamicElement = driver.find_element(NEWS_OPTION)"

"myDynamicElement = driver.find_element(NEWS_OPTION) "

"myDynamicElement = driver.find_element_by_id('blq-nav-news')"

"myDynamicElement = driver.find_element_by_id('blq-nav-news') "

,没有例外,脚本可以按预期工作.

, there is no exception and the script works as expected.

我发现根本原因是我们不使用

I figured out the root cause is we don't use

find_element_by _ *

find_element_by_*

.所以我想知道这是对Python-Webdriver的限制吗?我们是否有解决方案来解决我的问题而不改变我的代码.

. So I'd like to know this is limitation on Python-Webdriver ? whether we have a solution to fix my issue without changing my code as I did.

推荐答案

根据文档,您可以直接使用find_element_by_*快捷方式,也可以直接使用find_element()find_elements()私有"方法:

According to the documentation, you can use either find_element_by_* shortcuts, or find_element() and find_elements() "private" methods directly:

除了上面给出的公共方法外,还有两个私有方法 可能对页面对象中的定位器有用的方法.这些是 这两个私有方法:find_element和find_elements.

Apart from the public methods given above, there are two private methods which might be useful with locators in page objects. These are the two private methods: find_element and find_elements.

示例用法:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

但是,在您的情况下,您没有传递2个参数给find_element(),而是传递了一个参数-一个tuple NEWS_OPTION.您只需要将元组解压缩为位置参数:

But, in your case, instead of passing 2 parameters to find_element(), you are passing a single one - a tuple NEWS_OPTION. You just need to unpack the tuple into positional arguments:

NEWS_OPTION = (By.ID, 'blq-nav-news')
myDynamicElement = driver.find_element(*NEWS_OPTION)

或者,作为替代方案,您也可以使用关键字参数:

Or, just as an alternative, you could use keyword arguments also:

NEWS_OPTION = {'by': By.ID, 'value': 'blq-nav-news'}
myDynamicElement = driver.find_element(**NEWS_OPTION)


而且,每当您对事物的工作方式有任何疑问时,只需挖掘源代码并自己进行澄清即可.在这种情况下,请查看find_element_by_id()方法如何实际上已实现:


And, also, whenever you have any doubts how things should work, just dig up the source code and clarify it for yourself. In this case, see how find_element_by_id() method is actually implemented:

def find_element_by_id(self, id_):
    """Finds an element by id.

    :Args:
     - id\_ - The id of the element to be found.

    :Usage:
        driver.find_element_by_id('foo')
    """
    return self.find_element(by=By.ID, value=id_)

这篇关于出现错误“传入的无效定位器值".如果我们使用find_element而不是find_element_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆