簡體   English   中英

如何使用 python selenium 在 facebook 中點擊頁面

[英]how to click on the page in facebook using python selenium

這是我想進入的頁面 當我編譯我的代碼時,它會登錄,但是應該在頁面上單擊的行不起作用。 那是我的代碼:

from selenium import webdriver
from Config import keys
import time


def order(k):
   driver = webdriver.Chrome('driver\chromedriver.exe')
   driver.get(k['product_url'])
   username ="gmail"
   pw ="password"
   #Enter The Pw & email fb
   driver.find_element_by_xpath('//*[@id="email"]').send_keys(username)
   driver.find_element_by_xpath('//*[@id="pass"]').send_keys(pw)
   #Click on cnx button
   driver.find_element_by_xpath('//*[@id="u_0_b"]').click()
   #go to the page
     # here is the problem
   driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a').click()

   driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div/div[3]/div/div/div[1]/div/a/span').click()
   driver.page_source()


if __name__=='__main__':
    order(keys)

可能是由於同步問題嘗試誘導 WebDriverWait:

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='mount_0_0']/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='mount_0_0'']/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div/div[3]/div/div/div[1]/div/a/span"))).click()

注意:請將以下導入添加到您的解決方案中

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

在Facebook網站上查了一下, '//*[@id="mount_0_0"]'是主容器的xpath。 對於您嘗試單擊的按鈕,我發現的 xpath 是:

//*[@id="mount_0_0"]/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a

也許你可以試試這個?

暫無
暫無

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

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