Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
Python Selenium:等待WebElement中的文本更改_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium:等待WebElement中的文本更改

Python Selenium:等待WebElement中的文本更改,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我在Python2.7中使用selenium。从网页上的搜索框检索内容。搜索框动态检索并在框中显示结果 from selenium import webdriver from selenium.webdriver.common.keys import Keys import pandas as pd import re from time import sleep driver = webdriver.Firefox() driver.get(url) df = pd.read_csv("r

我在Python2.7中使用
selenium
。从网页上的搜索框检索内容。搜索框动态检索并在框中显示结果

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import re
from time import sleep

driver = webdriver.Firefox()
driver.get(url)

df = pd.read_csv("read.csv")

def crawl(isin):
    searchkey = driver.find_element_by_name("searchkey")
    searchkey.clear()
    searchkey.send_keys(isin)
    sleep(11)

    search_result = driver.find_element_by_class_name("ac_results")
    names = re.match(r"^.*(?=(\())", search_result.text).group().encode("utf-8")
    product_id = re.findall(r"((?<=\()[0-9]*)", search_result.text)
    return pd.Series([product_id, names])

df[["insref", "name"]] = df["ISIN"].apply(crawl)

print df
从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
作为pd进口熊猫
进口稀土
从时间上导入睡眠
driver=webdriver.Firefox()
获取驱动程序(url)
df=pd.read\u csv(“read.csv”)
def爬网(isin):
searchkey=驱动程序。按名称查找元素(“searchkey”)
searchkey.clear()
searchkey.发送密钥(isin)
睡眠(11)
搜索结果=驱动程序。按类名称(“ac结果”)查找元素
names=re.match(r“^.*(\())”,search\u result.text.group().encode(“utf-8”)
product_id=re.findall(r)((?您需要应用该概念。例如,等待元素变为可见:

在这里,它将等待10秒,每500毫秒检查一次元素的可见性

有一组内置的预期条件需要等待,编写您的应用程序也很容易


仅供参考,以下是我们在聊天中进行头脑风暴后的处理方法。我们引入了一个自定义预期条件,该条件将等待元素文本更改。它帮助我们确定新搜索结果何时出现:

import re

import pandas as pd
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import _find_element

class text_to_change(object):
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        actual_text = _find_element(driver, self.locator).text
        return actual_text != self.text

#Load URL
driver = webdriver.Firefox()
driver.get(url)

#Load DataFrame of terms to search for
df = pd.read_csv("searchkey.csv")

#Crawling function    
def crawl(searchkey):
    try: 
        text_before = driver.find_element_by_class_name("ac_results").text 
    except NoSuchElementException: 
        text_before = ""

    searchbox = driver.find_element_by_name("searchbox")
    searchbox.clear()
    searchbox.send_keys(searchkey)
    print "\nSearching for %s ..." % searchkey

    WebDriverWait(driver, 10).until(
        text_to_change((By.CLASS_NAME, "ac_results"), text_before)
    )

    search_result = driver.find_element_by_class_name("ac_results")
    if search_result.text != "none":
        names = re.match(r"^.*(?=(\())", search_result.text).group().encode("utf-8")
        insrefs = re.findall(r"((?<=\()[0-9]*)", search_result.text)
    if search_result.text == "none":
        names = re.match(r"^.*(?=(\())", search_result.text)
        insrefs = re.findall(r"((?<=\()[0-9]*)", search_result.text)
    return pd.Series([insrefs, names])

#Run crawl    
df[["Insref", "Name"]] = df["ISIN"].apply(crawl)

#Print DataFrame    
print df
重新导入
作为pd进口熊猫
从selenium导入webdriver
从selenium.common.Exception导入NoTouchElementException
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support.expected\u条件导入\u find\u元素
类文本更改(对象):
定义初始化(自身、定位器、文本):
self.locator=定位器
self.text=文本
定义呼叫(自身、驾驶员):
实际文本=\u查找元素(驱动程序,self.locator).text
返回实际文本!=self.text
#加载URL
driver=webdriver.Firefox()
获取驱动程序(url)
#加载要搜索的术语的DataFrame
df=pd.read\u csv(“searchkey.csv”)
#爬行功能
def爬网(搜索键):
尝试:
text\u before=驱动程序。通过类名称(“ac\U结果”)查找元素。text
除无任何例外:
text_before=“”
searchbox=驱动程序。按名称查找元素(“searchbox”)
searchbox.clear()
searchbox.send_key(searchkey)
打印“\n搜索%s…”%searchkey
WebDriverWait(驱动程序,10)。直到(
文本更改((By.CLASS\u名称,“ac\u结果”),文本更改之前)
)
搜索结果=驱动程序。按类名称(“ac结果”)查找元素
如果search_result.text!=“无”:
names=re.match(r“^.*(\())”,search\u result.text.group().encode(“utf-8”)

insrefs=re.findall(r)((?我建议在WebDriverWait中使用以下预期条件

WebDriverWait(driver, 10).until(
    text_to_be_present_in_element((By.CLASS_NAME, "searchbox"), r"((?<=\()[0-9]*)")
)
WebDriverWait(驱动程序,10)。直到(

文本在元素((By.CLASS\u NAME,“searchbox”),r“((?为等待条件创建类)中存在

class SubmitChanged(object):
    def __init__(self, element):
        self.element = element

    def __call__(self, driver):
        # here we check if this is new instance of element
        new_element = driver.find_element_by_xpath('<your xpath>')
        return new_element != self.element
类提交更改(对象):
定义初始化(自我,元素):
self.element=元素
定义呼叫(自身、驾驶员):
#这里我们检查这是否是元素的新实例
new_element=driver.find_element_by_xpath(“”)
返回新的元素!=self.element
在你的程序中调用它

     wait = WebDriverWait(<driver object>, 3)
     wait.until(SubmitChanged(<web element>))
wait=WebDriverWait(,3)
等待.直到(SubmitChanged())

更多信息在

这不是很容易,因为打开页面时,
searchbox
元素会立即加载。当我在元素中输入
searchkey
时,需要8-9秒才能加载元素中的文本内容。这就是我想要等待的内容。@Winterflags是的,我刚刚提供了示例和提示:)
text\u to\u be\u present\u in\u element
在您的情况下可能是一个很好的候选者,如果您知道要等待哪个文本。如果不知道,那么您将需要一个自定义的预期条件。非常感谢,我遵循了您对正则表达式的自定义预期条件,如下所示:。我设法在等待与模式匹配的第一个答复时获得它,但一旦它呈现的hat模式它继续运行循环搜索所有搜索键,但没有给它时间来调用它们。你有没有办法让它等待与相同模式匹配但不同的新内容?模式如下:
“Name ABC123(01234)”
“Something 123DEF(432134)”
某物某物123 GHI(07451)"
。不变的是,文本后面有一系列长度可变的数字,在括号内。多亏了聊天中的alecxe!解决了这个问题!非常有用。上面的自定义预期条件对于等待动态文本内容出现在WebElement中的Selenium用户来说非常有用。如果我没有弄错的话,这确实会很有用等待加载搜索框中的第一个文本回复。但是如果程序随后插入一个新的搜索词,它将从第一个结果识别模式,而不是等待加载第二个结果。请参阅我在“代码现在做什么”下的解释在OP.WebDriverWait中,我们使用的是显式等待的示例,这意味着我们需要在每个元素找到之前设置等待。这就是为什么我们在start中使用隐式等待,它将为每个元素找到设置等待。我认为最好是在这里使用sleep或编写函数来等待JQuery调用完成。
class SubmitChanged(object):
    def __init__(self, element):
        self.element = element

    def __call__(self, driver):
        # here we check if this is new instance of element
        new_element = driver.find_element_by_xpath('<your xpath>')
        return new_element != self.element
     wait = WebDriverWait(<driver object>, 3)
     wait.until(SubmitChanged(<web element>))