硒:为什么不执行按钮单击?

时间:2020-06-08 15:01:04

标签: python selenium

我想单击网站上的“搜索”按钮。然后打开下一页。单击不会执行(即使使用内置的time.sleeps(3)也不会执行),但是不会引发错误消息。

我在做什么错了?

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

driver = webdriver.Chrome('G:\\chromedriver\\chromedriver.exe')
driver.get("https://www.immobilienscout24.de")
driver.execute_script("return document.readyState") == "complete"


element = driver.find_element_by_id("oss-location")
#element.clear()
element.send_keys("10115 Berlin")

btn=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='oss-main-criterion oss-button button-primary one-whole']")))
#btn.click()#trial 1
#driver.execute_script("arguments[0].click();", btn) #trial 2
print("ready")

1 个答案:

答案 0 :(得分:1)

我建议在将值插入搜索框后等待自动搜索结果,然后单击自动搜索结果。然后单击treffer按钮。

代码

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

driver = webdriver.Chrome('G:\\chromedriver\\chromedriver.exe')
driver.get("https://www.immobilienscout24.de")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"oss-location"))).send_keys("10115 Berlin")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//li[@class='ui-menu-item'][contains(.,'10115 Berlin')])[1]"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='oss-main-criterion oss-button button-primary one-whole']//span[contains(.,'Treffer')]"))).click()