如何使用 python selenium 单击动态页面上的第一个结果?

How to click on the first result on a dynamic page using python selenium?

我试图点击 this 页面上的第一个结果,但我尝试的所有选项都不起作用。

首先,我使用电子邮件:kocianlukyluk@gmail.com 和密码:Redfinpython06 登录网站。这是它的代码:

driver = webdriver.Chrome("C:\Users\kocia\OneDrive\Plocha\Python\nastaveni\chromedriver.exe")
driver.get('https://www.redfin.com/myredfin/favorites')

email = 'kocianlukyluk@gmail.com'
password = 'Redfinpython06'

time.sleep(3)
driver.find_element_by_xpath(
    '//*[@id="content"]/div[6]/div/div[2]/div/div/form/span[1]/span/div/input').send_keys(email)

time.sleep(3)
driver.find_element_by_xpath(
    '//*[@id="content"]/div[6]/div/div[2]/div/div/form/span[2]/span/div/input').send_keys(password)

time.sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()

但是问题是登录后我无法点击页面上的第一个结果

这是我尝试过的:

result = driver.find_elements_by_xpath("//*[@id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.find_element_by_xpath("//*[@id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]").click()

result = driver.find_elements_by_xpath("//*[@id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.click()

result = driver.find_element_by_xpath("//*[@id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]")
result.click()

非常感谢您的帮助。

我希望这是您仅用于测试目的的虚拟电子邮件和密码:)

点击下方列表中的第一张房屋图片。我还清理了您的 emailpassword xpath 名称。您可以通过 name

来了解抓取它们有多容易

此外,您可能希望在这些查找元素周围放置适当的等待方法。一般不推荐使用sleep

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


driver = webdriver.Chrome()
driver.get('https://www.redfin.com/myredfin/favorites')

email = 'kocianlukyluk@gmail.com'
password = 'Redfinpython06'

sleep(3)
driver.find_element_by_name(
    'emailInput').send_keys(email)

sleep(3)
driver.find_element_by_name(
    'passwordInput').send_keys(password)

sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()
sleep(3)

first_house = driver.find_element_by_xpath("//div[@class='FavoritesHome'][1]//img")
first_house.click()