我正在使用Selenium Python创建Twitter自动追随者机器人

时间:2019-05-03 17:01:59

标签: python selenium

我正在为Twitter创建一个机器人。

代码说明(可选阅读):

我的代码将要执行的操作是,它将仅转到某个目标URL并跟踪所有用户。就像如果某个用户的追随者过多,那么我的机器人将只打开他的追随者列表,并在此关注他们。如果您仍然无法理解我要说的话,请离开,这对于回答我的问题不是必不可少的

我的代码

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome("D:\Projects\Python Projects\Automation\Web Drivers\chromedriver.exe")

driver.maximize_window()
driver.get("https://twitter.com/")

# Clicking on login button to go to login page
while True:
    try:
        driver.find_element_by_xpath('//*[@id="doc"]/div/div[1]/div[1]/div[2]/div[2]/div/a[2]').click()
        print("Successfully Clicked on login button to go to  Login Page")
        break
    except:
        print("Unable to Go to Login Page, Retrying...")
        continue

while True:
    try:
        driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input')
        print("Gone to Login Page Successfully")
        break
    except:
        continue

# Entering Login Information

# Entering Email address
driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input').send_keys("email address")

#Entering Password
driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/fieldset/div[2]/input').send_keys("password")

#Clicking on Login Button
driver.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button').click()
print("Login Information Entered Successfully")


# Entering page to follow users
while True:
    try: #Going to some target url
        driver.get("https://mobile.twitter.com/online_amna/followers")
        print("Link GOT Successfully")
        break
    except:
        continue


'''This is just for checking if the followers window is loaded or not, common sense if window is loaded then the following element should be there. Just ignore this'''

while True:
    try:
        driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div/main/div/div[2]/div/div/div/div/div[2]/section')
        print ("page Loaded")
        break
    except:
        continue

sleep(2)

i  = 1 

'''As we see, when we scroll down, then only page loads more so to do that I grabbed the main Div element to apply some javascript and scroll down the page. This is javascript selector I stored as a string in element variable'''


element = 'document.querySelector("#react-root > div > div > div > main > div > div.css-1dbjc4n.r-aqfbo4.r-1niwhzg.r-16y2uox > div > div > div > div > div:nth-child(2) > section")'

问题在这里*

while True:
#This try block should check if the div is available there or not..Div means that in twitter markup, one user has its main div in which its info and a button is there to follow him etc so I am simply checking that div.'''

    try:    
        driver.find_element_by_css_selector(f'#react-root > div > div > div > main > div > div.css-1dbjc4n.r-aqfbo4.r-1niwhzg.r-16y2uox > div > div > div > div > div:nth-child(2) > section > div > div > div > div:nth-child({i}) > div')
        print(f'Value of I = {i}')
        i += 1
        continue
'''If div is not available then I will use this javascript code to load more by scrolling down.'''
    except:
        driver.execute_script(f'{element}.scrollIntoView(false)')
        continue

预期结果: 我原以为当第一次运行while循环时,它将遍历所有可用的div,并且可以正常工作,但是当div结束时,我的意思是我告诉我们必须向下滚动以加载更多用户来关注他们,所以我原本期望except块将运行并滚动屏幕,结果将加载更多用户。也可以。

问题:

问题是我期望它在加载新用户时会再次运行 try 块,并且应该执行 try 块,因为新用户已加载...但它不会遍历新用户,而是一次又一次地运行,除了阻塞一次。

我知道很难理解我想说什么,您只需复制我的代码并粘贴并查看结果,即可轻松理解我想说的话

0 个答案:

没有答案