简体   繁体   中英

how to click on the page in facebook using python selenium

这是我想进入的页面 when i compile my code it log in but the line that should click on the page does not work. thats my code:

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)

Could be due to synchronization issue try to induce 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()

Note: please add below imports to your solution

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

After checking on the Facebook website, '//*[@id="mount_0_0"]' is the xpath of the main container. For the button you are trying to click, the xpath I have found is:

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

Maybe you could try with this one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM