簡體   English   中英

如何使用 python selenium 在動態頁面上單擊第一個結果?

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

我正在嘗試單擊頁面上的第一個結果,但我嘗試的所有選項均無效。

首先,我只需使用 email 登錄網站: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()

非常感謝您的幫助。

我希望這是您僅用於測試目的的虛擬 email 和密碼:)

下面點擊列表中的第一張房子圖片。 我還清理了您的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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM