无法解析网页中的某些文本

时间:2018-07-24 22:42:54

标签: python python-3.x selenium selenium-webdriver web-scraping

我已经用python用selenium编写了一个脚本,以从网页中抓取一些文本。我想抓取的文本是在填写输入框后生成的。我的脚本可以用正确的方式填写并填充值。但是,它无法解析文本。我该怎么办?

这是我到目前为止尝试过的:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("http://dev.delshlearning.com.au/test.php")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#AM"))).send_keys("`(2(3^5-sqrt(3)))/2`",Keys.RETURN)

item = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#MQ"))).text
print(item)
driver.quit()

send_keys()参数已经填写在脚本中供您考虑。

1 个答案:

答案 0 :(得分:3)

它将 text 值存储在属性value中。这应该起作用:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("http://dev.delshlearning.com.au/test.php")

# I changed your locater to ID since it's a little more clear
wait.until(EC.visibility_of_element_located((By.ID,"AM"))).send_keys("`(2(3^5-sqrt(3)))/2`",Keys.RETURN)
item = wait.until(EC.visibility_of_element_located((By.ID,"MQ"))).get_attribute('value')
print(item)
driver.quit()

我通过转到元素的 properties 找到了它,如下所示: enter image description here