Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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/8/selenium/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
Python 网页的格式使使用selenium选择文本变得不可能_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 网页的格式使使用selenium选择文本变得不可能

Python 网页的格式使使用selenium选择文本变得不可能,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,这个问题快把我逼疯了:我试图用Selenium捕捉一个机器人的响应,但尽管我可以输入文本并让机器人回复,但它的网页的格式使得选择输出文本成为一场噩梦 这是我用Python编写的代码: from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep driver = webdriver.Firefox() driver.get("http://demo.vh

这个问题快把我逼疯了:我试图用Selenium捕捉一个机器人的响应,但尽管我可以输入文本并让机器人回复,但它的网页的格式使得选择输出文本成为一场噩梦

这是我用Python编写的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Firefox()
driver.get("http://demo.vhost.pandorabots.com/pandora/talk?botid=b0dafd24ee35a477")
elem = driver.find_element_by_name("input")
elem.clear()
elem.send_keys("hello")
elem.send_keys(Keys.RETURN)

line = driver.find_element_by_xpath("(//input)[@name='botcust2']/preceding::font[1]/*")


print(line)
response = line.text
print(response)

driver.close()
它成功地得到了响应的第一位(“乔姆斯基:”),但没有得到其余的

如何正确捕获响应文本(理想情况下不包括bot名称)? 有没有一种更优雅的方法(如jquery脚本)在网页重新格式化时不会轻易崩溃

非常感谢

编辑

因此,在对jQuery进行了更多的研究之后,我找到了一种解决任何URL文本都无法显示的方法

我将整个文本字符串设置为一个变量,然后用“”替换名称和空行的任何实例。因此,pguardiario指出的jQuery代码变成:

# get the last child text node
response = self.browser.execute_script("""
                  var main_str = $('font:has(b:contains("Chomsky:"))').contents().has( "br" ).last().text().trim();
                  main_str = main_str.replace(/Chomsky:/g,'').replace(/^\\s*[\\r\\n]/gm, '');
                  return main_str;
                """)
我相信可能会有更好/更优雅的方法来完成这件事,但现在它起作用了


非常感谢pguardiario和其他人的建议

既然您要求jQuery:

from requests import get
body = get("http://code.jquery.com/jquery-1.11.3.min.js").content.decode('utf8')
driver.execute_script(body)

# get the last child text node
response = driver.execute_script("""
  return $('font:has(b:contains("Chomsky:"))').contents().last().text().trim()
""")

若要使用文本节点中的响应来捕获来自的响应,您可以
执行\u script()
方法,如下所示:

  • 代码块:

    driver.get('http://demo.vhost.pandorabots.com/pandora/talk?botid=b0dafd24ee35a477')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='input']"))).send_keys("hello")
    driver.find_element_by_css_selector("input[value='Ask Chomsky']").click()
    print(driver.execute_script("return arguments[0].lastChild.textContent;", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@value='Ask Chomsky']//following-sibling::font[last()]//font")))).strip())
    
  • 控制台输出:

    Hi! Can I ask you a question?
    

它是否可以进行任何API调用?您是否可以获取字体元素的文本(而不是排除字体元素文本的子元素),将其剥离,然后手动删除前面的“Chomsky:”命令?该页面似乎没有进行任何我可以使用的API调用。如果我能以编程方式剥离前面的“Chomsky”部分,那么直接从font元素获取文本就很好了,因为我只需要将纯文本传递到一个text-to-speech变量。看起来很接近了。html不是超级友好的,但是您已经用xpath捕获了父元素:(//input)[@name='botcust2']/previous::font[1]现在您只需要捕获该元素下的文本元素。这对文本非常有用,但是当bot与URL一起输出文本时,它会中断。如果您将问题设置为“告诉我关于瑞典的情况”,它将省略url文本(瑞典),并输出“是北欧的一个国家,与芬兰和挪威接壤”。同样,如果您查询“告诉我更多关于瑞典的情况”,它将不会输出任何内容(机器人提供带有url和图像的详细响应).在这种情况下,也许只需将字体文本和正则表达式从“Chomsky:”