- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在使用 python splinter library测试一个网络应用程序,当我检查一个元素是否存在并且我手动找到每个元素来操作它时,我遇到了一个问题。
问题是,当输入列表大于 4 项或更多时,遇到元素不存在的情况时,需要 12 秒以上的时间才能完成。
我也试过设置 wait_time=1,但如果输入列表大于 10,如果该元素在页面上的任何地方都不存在,总共需要 10 次。
for i in inputs:
if browser.element_exists():
elm = browser.find_element():
elm.text()
我需要一些方法来加快速度,以便并行进行元素检查,而不是一个一个地进行。我唯一能想到的就是执行我不喜欢的 javascript(我想将其全部保留在 python 中)。
def get_columns(current_depth,step,element):
columns = []
for xpath in xpaths:
what = parse_xpath(row[2])
if browser.is_element_present_by_xpath(xpath,wait_time=1):
element = browser.find_by_xpath(xpath)
columns.append(element.text)
else:
columns.append('none')
return columns
最佳答案
is_element_present_by_xpath
代码是
def is_element_present_by_xpath(self, xpath, wait_time=None):
return self.is_element_present(self.find_by_xpath, xpath, wait_time)
使用
def is_element_present(self, finder, selector, wait_time=None):
wait_time = wait_time or self.wait_time
end_time = time.time() + wait_time
while time.time() < end_time:
if finder(selector):
return True
return False
和
def find_by_xpath(self, xpath, original_find=None, original_query=None):
original_find = original_find or "xpath"
original_query = original_query or xpath
return self.find_by(self.driver.find_elements_by_xpath,
xpath,
original_find=original_find,
original_query=original_query)
您的代码基本上似乎使用了同一个函数两次。第一次使用 find_by_xpath
浏览列表以测试元素是否在其中后,您可以第二次执行此操作以查找元素。
find_by_xpath
返回 ElementList .
ElementList 有一个方法 is_empty()
如果它是空的也就是不匹配则返回 True。
那又如何呢(我没有测试过,但从我的脑海中回答)。
def get_columns(current_depth,step,element):
columns = []
for xpath in xpaths:
what = parse_xpath(row[2])
element_list = browser.find_by_xpath(xpath)
# You might want to check that your element_list has only 1 element.
if element_list.is_empty():
columns.append('none')
else:
columns.append(element_list[0].text)
return columns
这样可以避免遍历列表两次。
2014-06-20 更新:
走下兔子洞。 Splinter 正在调用 selenium wedriver ... self.find_by(self.driver.find_elements_by_xpath, ...
这是另一个初始级别。我不确定他们为什么决定这样做而不是在第一个直接调用 selenium地方。
仅出于测试目的,您应该首先尝试使用代码的这一特定部分直接使用 selenium,看看您是否注意到性能有很大差异。如果性能问题来自 splinter 或 selenium,这将有所区别。
在 selenium/webdriver/remote/webdriver.py 中
```def find_elements_by_xpath(self, xpath): """ 通过 xpath 查找多个元素。
:Args:
- xpath - The xpath locator of the elements to be found.
:Usage:
driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")
"""
return self.find_elements(by=By.XPATH, value=xpath)
```
正在使用:
``` def find_elements(self, by=By.ID, value=None): """ find_elements_by_* 方法使用的“私有(private)”方法。
:Usage:
Use the corresponding find_elements_by_* instead of this.
:rtype: list of WebElement
"""
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")
return self.execute(Command.FIND_ELEMENTS,
{'using': by, 'value': value})['value']
```
最后一个是通过 JsonWireProtocol 直接调用 API。这里的性能高度依赖于 Selenium 在您的系统和/或浏览器上的实现(如果您正在使用的话)。
2014-06-20 更新 2:
另请注意,特别是对于 XPath,它实际上取决于所使用的驱动程序。 Selenium documentation about this specific search :
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.
关于python - 碎片:更快的查找元素的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145026/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!