Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Selenium Python绑定中等待并获取Span对象的值_Python_Unit Testing_Selenium_Assertions_Testcase - Fatal编程技术网

如何在Selenium Python绑定中等待并获取Span对象的值

如何在Selenium Python绑定中等待并获取Span对象的值,python,unit-testing,selenium,assertions,testcase,Python,Unit Testing,Selenium,Assertions,Testcase,我的网页中有以下代码 <div id="" class="user_acc_setails"> <ul id="accDtlUL"> <li>First Name: <span id="f_name">Anuja</span></li> 但是它给了我一个assessionerror。我怎样才能解决这个问题 谢谢,我找到了解决办法。也许这不是正确的方法。我所做的是在查找元素并断言其值之前使用Python睡眠 import t

我的网页中有以下代码

<div id="" class="user_acc_setails">
<ul id="accDtlUL">
<li>First Name: <span id="f_name">Anuja</span></li>
但是它给了我一个assessionerror。我怎样才能解决这个问题


谢谢,我找到了解决办法。也许这不是正确的方法。我所做的是在查找元素并断言其值之前使用Python睡眠

import time
...

time.sleep(3)
element = context.browser.find_element_by_id('f_name')
assert element.text == 'Anuja'

然后它就完美地工作了。

本例中使用它的正确方式(请参见此处的Python代码)。所以你需要像

from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.text_to_be_present_in_element((By.Id,'f_name'), 'Anuja'))
您可以使用browser.implicitly\u wait(2)命令隐式地等待浏览器。
这里有一个函数可以做到这一点

def wait_on_element_text(self, by_type, element, text):
        WebDriverWait(self.driver, 10).until(
            ec.text_to_be_present_in_element(
                (by_type, element), text)
        )
其中by_类型替换为例如by.XPATH、by.CSS_选择器。 其中元素替换为元素路径-元素xpath、唯一选择器、id等。 其中,文本替换为元素的文本,例如,与元素关联的字符串

url = "http://..."
driver = webdriver.Firefox()
driver.get(url)

wait = WebDriverWait(driver, 10)
try:
    present = wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, "myclassname"), "valueyouwanttomatch"))
    elem = driver.find_element_by_class_name("myclassname")
    print elem.text
finally:
driver.quit()

我意识到
的返回对象wait.until不是一个elem而是一个布尔变量,因此我必须再次调用locate元素。

不,它不是,显式等待是问题的答案:是否要选择一个答案?这只会减少默认的隐式等待。如果类名重复,但文本不同,如何在元素中获得
text_to_be_present_
?作为一种解决方法,我一直在使用
presence\u of_element\u located((By.CLASS\u NAME,“repeatclassname”))
,但我更喜欢使用特定值引用重复类的位置。
def wait_on_element_text(self, by_type, element, text):
        WebDriverWait(self.driver, 10).until(
            ec.text_to_be_present_in_element(
                (by_type, element), text)
        )
url = "http://..."
driver = webdriver.Firefox()
driver.get(url)

wait = WebDriverWait(driver, 10)
try:
    present = wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, "myclassname"), "valueyouwanttomatch"))
    elem = driver.find_element_by_class_name("myclassname")
    print elem.text
finally:
driver.quit()