【发布时间】:2021-10-22 02:25:46
【问题描述】:
我想使用 Selenium 和 Gauge 从网页中获取数据。我使用wait_for_element编写了以下代码:
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from step_impl.mixins.exception import MixinException
class MixinSelector(MixinException):
def __init__(self, driver=None):
super(MixinSelector, self).__init__(driver)
self.wait_for_element_time = 60
def wait_for_elem(self, by, locator_string):
try:
element = WebDriverWait(self.driver,self.wait_for_element_time)
.until(EC.presence_of_element_located((by,
locator_string)))
print(element)
print(type(element))
return element
except TimeoutException:
self.fail_test('failed')
except:
raise
然而,这会产生一个dict 实例,如调试prints 所示:
{'ELEMENT': '0.8884832448581164-1'}
<class 'dict'>
我这样调用函数:
self.wait_for_elem(By.CSS_SELECTOR, '#baseWebLayoutContainerID')
这是一个问题,因为我需要将结果作为可以在其他几个功能中使用的 Web 元素。我怎样才能得到这个结果?
【问题讨论】:
-
您究竟想要取回什么?那有什么不同,为什么这个结果对你不可用? “元素”到底是什么意思?例如,您是否需要某个特定类的实例?
-
我将您的问题编辑为remove noise 并明确提出问题。请参阅How to Ask 获取更多指导。
-
一个网络元素,我现在意识到我没有指定它
-
另请阅读stackoverflow.com/help/minimal-reproducible-example。我们看不到这样的问题,因为在其他可能的问题中,
EC没有在本地定义。 -
完成。我很抱歉,有点新。将阅读“如何提问”及其文档。
标签: python selenium-webdriver webdriverwait