Python Selenium检查是否有新元素可用



我正在尝试制作一个机器人程序,检查网站上何时有新房,这样我就可以使用selenium的点击事件。

我如何跟踪这个列表,并在网站上添加新房时进行检查?我已经创建了一个列表,里面充满了可用房屋的元素,但不知道如何从这里走得更远。。

谢谢你的帮助!

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait

def create_driver():
options = Options()
options.add_experimental_option("detach", True)
service = Service(executable_path="/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=service, chrome_options=options)

return driver
def login(driver):
driver.find_element(By.NAME, 'txtEmail').send_keys('XXXXXXXXX@gmail.com') 
driver.find_element(By.NAME, 'txtWachtwoord').send_keys('XXXXXXX')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="CybotCookiebotDialogBodyButtonAccept"]'))).click()
#driver.find_element(By.ID, 'CybotCookiebotDialogBodyButtonAccept').click()
driver.find_element(By.CLASS_NAME, 'btn-secondary').click()

def search_page(driver):    
driver.find_element(By.XPATH,"//a[@href='/zoekopdracht/']").click()


def list_of_woningen(driver):
woningen = []
woningLijst = driver.find_elements(By.CLASS_NAME,"stretched-link")
for x in woningLijst:
woningen.append(x.text)
return woningen




def start_session(driver,url):    
driver.get(url)
login(driver)
search_page(driver)
list_of_woningen(driver)


#return driver.page_source
if __name__ == '__main__':
start_session(create_driver(),'https://hurenbij.vesteda.com/')









我不知道是否有一个事件列表器可以部署或类似的东西,但你可以手动执行,你可以每隔30秒左右重新检查是否发生了任何更改。

def check_for_changes(driver, old_list):
print("Checking for changes")
new_list = list_of_woningen(driver)
if old_list == new_list:
print("No changes found")
return False
else:
print("Changes found")
print("Old list: ", old_list)
print("New list: ", new_list)
return True

def start_session(driver, url):
driver.get(url)
login(driver)
search_page(driver)
old_list = list_of_woningen(driver)
while not check_for_changes(driver,old_list):
sleep(30) # 30 seconds
# a better approach for this, is to make a wait thread, if there are other functionalities for tha program 
driver.refresh()
else:
print("ALERT!!!")

同样,使用内置的selenium事件侦听器可能会有更好的解决方案,但这是一个有效的解决方案。

相关内容

  • 没有找到相关文章

最新更新