lundi 22 février 2021

How to Scroll Inside Element Python Selenium?

my question is.. I'd like to get the usernames of people who liked a specific tweet on Twitter and put them in a list. Here's the part that I have no problem. I know.. it's messy for now, but I'll clean it up when I'm able to do scrolling down in the section of 'Liked by'.After I scroll down to the end of the 'Liked By' section, I'm planning to do get the usernames with beautifulsoup.. thank you in advance for any help.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import time
path =r'YOUR PATH'
url ='https://twitter.com/login'
driver =webdriver.Chrome(executable_path=path)
driver.get(url)
#******************** 

search ='THE ACCOUNT YOU WANT TO GO'

time.sleep(2)
username =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input')
password =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input')
name ='USERNAME'
password_2='PASSWORD'
enter =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div')

#slow typing for username and password
for i in name:
    username.send_keys(i)
    time.sleep(0.4)
time.sleep(1)

for j in password_2:
    password.send_keys(j)
    time.sleep(0.4)
    
enter.send_keys(Keys.ENTER) 


#element =WebDriverWait(driver,10).until(
#EC.presence_of_element_located((By.XPATH,'//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[2]/div/div/div/form/div[1]/div/div/div[2]'))) 


time.sleep(2)
search_icon =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/header/div/div/div/div[1]/div[2]/nav/a[2]')
search_icon.click()
time.sleep(1)
search_bar =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[2]/div/div/div/form/div[1]/div/div/div[2]/input')

#slow typing(again) it types 'search' variable
for i in search:
    search_bar.send_keys(i)
    time.sleep(0.4)

search_bar.send_keys(Keys.RETURN) 
time.sleep(2)

#click people section and choose the first one
people_icon =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[1]/div[2]/nav/div/div[2]/div/div[3]/a')
people_icon.click() 
time.sleep(2)
profile =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/section/div/div/div[1]/div')
profile.click()

time.sleep(1)

WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.XPATH,'//*[@id="react-root"]/div/div/div[2]/main')))
#( wait until things get load- max 10 sec.)


tweets =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/nav/div/div[2]/div/div[2]')
tweets.click() #click tweets&replies section
time.sleep(2)

post =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/div[2]/section/div/div/div[1]/div/div/article/div/div/div/div[2]/div[1]')
post.click() #latest post

time.sleep(2)

number_of_likes =driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/section/div/div/div[1]/div/div/article/div/div/div/div[3]/div[4]/div/div[3]/div/a/div').text


people_who_liked = driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/section/div/div/div[1]/div/div/article/div/div/div/div[3]/div[4]/div/div[3]/div')
people_who_liked.click() #access to the people who liked

Aucun commentaire:

Enregistrer un commentaire