无法通过Selenium与Python交互的输入元素(WebDriverWait和ActionChains不可用)

2024-04-19 20:13:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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: &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, 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: &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Arial, Helvetica, sans-serif; left: -9em;">
</div>

我可以找到输入的web元素,但是任何操作,比如click/sendkeys都会给ElementNotVisibleException(消息:element not interactible)。在

^{pr2}$

输入元素应该是可见的,因为我可以从元素中获得类似样式的属性。在

  startTimeElem.get_attribute("style")

我试过一些办法,但没有一种可行。在

  1. 使用WebDriverWait等待可单击

如果我试图等待元素可单击,将引发“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"]')))
  1. 使用ActionChains。在

代码可以正常运行,但输入文本没有如预期的那样更改为“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。在

任何帮助都将不胜感激。在


Tags: namefromimportbrowsersend元素inputstyle