我想用Selenium编写一个python程序,从一个网站https://en.midland.com.hk/property-price-chart获取数据,并从房地产价格表中输入年份的变化开始。我会用“startTimeElem”和“endTimeElem”来表示它们。在
下面是我需要输入的元素的HTML代码:
<div style="position: relative; height: 0px; z-index: 1;">
<input name="min" class="highcharts-range-selector" type="text" style="top: 86px; position: absolute; border: 0px; width: 1px; height: 1px; padding: 0px; text-align: center; font-size: 12px; font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, sans-serif; left: -9em;">
<input name="max" class="highcharts-range-selector" type="text" style="top: 86px; position: absolute; border: 0px; width: 1px; height: 1px; padding: 0px; text-align: center; font-size: 12px; font-family: "Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, sans-serif; left: -9em;">
</div>
我可以找到输入的web元素,但是任何操作,比如click/sendkeys都会给ElementNotVisibleException(消息:element not interactible)。在
^{pr2}$输入元素应该是可见的,因为我可以从元素中获得类似样式的属性。在
startTimeElem.get_attribute("style")
我试过一些办法,但没有一种可行。在
如果我试图等待元素可单击,将引发“ElementNotVisibleException:消息:元素不可交互”。在
startTimeElem = WebDriverWait(browser, 5).until(EC.element_to_be_clickable((By.XPATH, '//input[@class="highcharts-range-selector"][@name="max"]')))
endTimeElem = WebDriverWait(browser, 5).until(EC.element_to_be_clickable((By.XPATH, '//input[@class="highcharts-range-selector"][@name="max"]')))
代码可以正常运行,但输入文本没有如预期的那样更改为“2000-01”。在
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(browser).move_to_element(startTimeElem).click().send_keys(Keys.BACK_SPACE).send_keys(Keys.CONTROL+ "a").send_keys("2000-01").send_keys(Keys.ENTER).perform()That's four spaces for the list
以下是我写的完整代码:
from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
# get the path of ChromeDriverServer
chrome_driver_path = "chromedriver\chromedriver.exe"
browser = webdriver.Chrome(chrome_driver_path)
# extract pricing data from the website of Midland
url_midland="https://en.midland.com.hk/property-price-chart/"
browser.get(url_midland)
time.sleep(5)
startTimeElem = browser.find_element_by_xpath('//input[@class="highcharts-range-selector"][@name="min"]')
endTimeElem = browser.find_element_by_xpath('//input[@class="highcharts-range-selector"][@name="max"]')
browser.execute_script("arguments[0].style.visibility = 'visible'",startTimeElem)
# I can access the "style" of startTimeElem
print(startTimeElem.get_attribute("style"))
startTimeElem.click()
startTimeElem.send_keys(Keys.BACK_SPACE)
startTimeElem.send_keys(Keys.CONTROL+ "a")
startTimeElem.send_keys(Keys.DELETE)
startTimeElem.send_keys("2000-01")
希望房地产价格图表的年份能够改变,“从”变为“2000-01”,“结束”变为“2018-12”。初始期限为“2018-03”至“2019-03”。 我需要在多个场景中提取住房数据,因此需要使用Selenium。在
任何帮助都将不胜感激。在
目前没有回答
相关问题 更多 >
编程相关推荐