gpt4 book ai didi

python - 碎片:更快的查找元素的方法?

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:12 24 4
gpt4 key购买 nike

所以我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com