Selenium findElement(by by)在Python中等效 [英] Selenium findElement(By by) equivalent in Python

查看:425
本文介绍了Selenium findElement(by by)在Python中等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Selenium(Python),如何将By对象传递给findElement()?

Using Selenium (Python), how do we pass the By object to findElement()?

Java (有效)

By locater = By.id("username")
WebElement elem = driver.findElement(locater)
elem.SendKeys("tester")

Python (此操作失败)

locater = By.id("username")
elem = driver.find_element(locater)
elem.send_keys("tester")

我在python中遇到的错误是'str' object is not callable. 我在其他SO对话中查找了此内容,原因是python期望使用By.ID或By.XPATH等.

Error i get in python is 'str' object is not callable. I looked this up in other SO conversations and its because python expects something like By.ID or By.XPATH etc.

我需要一种传递By对象的方法,并且想知道是否有可能.预先感谢.

I need a way to pass the By object and wondered it it is possible. Thanks in advance.

推荐答案

Python中,By.XPATH不是类By()的方法,而是字符串变量:

In Python By.XPATH is not a method of class By(), but string variable:

By.XPATH == "xpath"

尝试实现以下代码:

from selenium.webdriver.common.by import By

locator = (By.XPATH, oSignUp.listformfieldxpaths[0])
elem = oDriver.getdriver().find_element(*locator)
elem.send_keys("tester")

请注意,find_element()应该接收2个参数:byvalue.两者都是strings

Note that find_element() should receive 2 arguments: by and value. Both are strings

这篇关于Selenium findElement(by by)在Python中等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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