0

I'm fluent with using python selenium, but it didn't work on the new facebook design page

enter image description here

If anyone have the new facebook page please check if you can detect any element using python on this page : https://www.facebook.com/messages/t/

for example looking for this xpath : //li[@class='_5l-3 _1ht1 _6zk9'], in browser we'll find 30 elements. but in python when we run our script it won't work :

enter image description here

here is my code :

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_experimental_option("prefs", {
        "profile.default_content_setting_values.media_stream_mic": 2,
        "profile.default_content_setting_values.media_stream_camera": 2,
        "profile.default_content_setting_values.geolocation": 2,
        "profile.default_content_setting_values.notifications": 2
    })
global driver
driver = webdriver.Chrome("yourpathtochromedriver", options=opt)

time.sleep(5)
driver.get("https://www.facebook.com/")

username = driver.find_element_by_xpath("//input[@name='email']")
username.send_keys('youremail')
username = driver.find_element_by_xpath("//input[@name='pass']")
username.send_keys('yourpassword')
driver.find_element_by_xpath("//button[@name='login']").click()

driver.get("https://www.facebook.com/messages/t")
time.sleep(3)

test = driver.find_elements_by_xpath("//li[@class='_5l-3 _1ht1 _6zk9']")
count = (len(test))
print(count)
Sarah Guegan
  • 23
  • 1
  • 6

2 Answers2

1

I have run into similar issues before and what worked for me was using the WebDriverWait class. From my experience, this issue is happening because you are trying to retrieve the elements from the page before it is even loaded. You can use WebDriverWait to wait for the presence of the element to be detected then retrieve it.

0
  1. I fail to find element "//li[@class='_5l-3 _1ht1 _6zk9']" on page https://www.facebook.com/messages/t/.
    So provide some more error log please.

  2. it is not a good idea "//li[@class='_5l-3 _1ht1 _6zk9']", try something like this "//li[contains(@class,'_5l-3') and contains(@class,'_1ht1') and contains(@class,'_6zk9')]", or cssSelector expression "li._5l-3._1ht1._6zk9".

Peter Quan
  • 788
  • 4
  • 9