2 回答
TA贡献1900条经验 获得超5个赞
在 search_box 提交后,您需要使用网络驱动程序等待等待页面加载。
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'MapHomeCard_0')))
还导入以下内容
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TA贡献1871条经验 获得超8个赞
要定位您需要引发WebDriverWait 的元素visibility_of_element_located()
,您可以使用以下任一定位器策略:
使用XPATH:
driver.get('https://www.redfin.com')
search_form = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search-input-box' and @id='search-box-input'][@title='City, Address, School, Agent, ZIP']")))
search_form.send_keys("20007")
search_form.submit()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='homecards']/div[@id='MapHomeCard_0']"))).text)
使用CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.homecards>div#MapHomeCard_0"))).text)
控制台输出:
LISTED BY REDFIN
3D WALKTHROUGH
$464,000
1 Bed
1 Bath
1,014 Sq. Ft.
2500 Q St NW #412, Washington, DC 20007
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
添加回答
举报