python selenium如何复制网站文字?

时间:2017-05-10 07:40:43

标签: python selenium

如何复制文字使用pythonselenium加载页面后。 网站的Html代码是:

<html>
<head>
<link rel="alternate stylesheet" type="text/css" href="resource://gre-resources/plaintext.css" title="Wrap Long Lines">
</head>
<body>
<pre>32 251 305 266 0.992355
    29 216 480 231 0.985983
    152 76 825 92 0.983704
    116 157 871 173 0.982307
    26 181 535 197 0.971898
    26 76 137 138 0.964413
    25 17 682 40 0.961533
    150 99 815 115 0.910050
    101 118 242 142 0.783791
    102 151 144 158 0.676560
    </pre>
</body>
</html>

我的代码来源:

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

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


i=0 
driver = webdriver.Firefox()
driver.get('http://www.textdet.com/')

e = driver.find_element_by_id("imagefile")                  
e.send_keys("/home/brm17/Desktop/ProjetFinDetude/image.png")  



button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Download bounding boxes")]')))
button.click()

我想在&lt;中获取文本pre&gt; ...&lt; / pre&gt; 我该怎么办 ?

1 个答案:

答案 0 :(得分:0)

您只是靠近输出,您只需要获得具有文本并打印相同的标签。

示例:
根据您的代码

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

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


i=0 
driver = webdriver.Firefox()
driver.get('http://www.textdet.com/')

e = driver.find_element_by_id("imagefile")                  
e.send_keys("/home/brm17/Desktop/ProjetFinDetude/image.png") 
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Download bounding boxes")]')))
button.click()
mytext = driver.find_element_by_xpath('/html/body/pre')
print mytext.text

这会将pre标记的内容打印为文本