[英]Pyhton Selenium unintentional click problem
我試圖用 python selenium 來吸引追隨者。 但有時 python 會自行點擊。 我想做一個沒有錯誤的程序。 我嘗試嘗試過“try catch”構造,但沒有奏效。 這是我的代碼:
def getFollowers(self):
try:
self.browser.get(f"https://www.instagram.com/{self.username}")
time.sleep(2)
followers=self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[2]/a").click()
time.sleep(2)
dialog=self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]")
followerCount=len(dialog.find_elements_by_tag_name("li"))
print(f"first count:{followerCount}")
action=webdriver.ActionChains(self.browser)
//*******************************************Probly my problem is here****************************************
while True:
dialog.click()
action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(3)
newCount=len(dialog.find_elements_by_tag_name("li"))
if followerCount!=newCount or newCount==24:
print(f"New count:{newCount}")
time.sleep(3)
followerCount=newCount
else:
break
//**********************************************************************************************************
followers=dialog.find_elements_by_tag_name("li")
followersList=[]
for user in followers:
link=user.find_element_by_css_selector("a").get_attribute("href")
# print(link)
followersList.append(link)
with open("followers.txt","w",encoding="UTF-8") as file:
for item in followersList:
file.write(item+"\n")
time.sleep(5)
except:
pass
我也有 def getfollowing,它完美無缺。 如果你願意,我也可以展示它。 但它們幾乎相同。
編輯:@RohanShah 解決了我的問題。 在頁面底部,您可以看到解決方案。
編輯:我是新來的,這就是為什么有時我的問題可能毫無意義。但請不要降低我的分數。 Stackoverflow 不會再接受我的問題了。 請增加我的積分。
我在滾動彈出窗口時遇到了同樣的問題。 發生的情況是您的dialog.click()
,在嘗試將您的鍵向下放在彈出窗口上時,偶爾會單擊用戶並加載他們的個人資料。 當彈出窗口不再出現在屏幕上時,您的腳本就會崩潰。
在對解決這個問題進行了大量研究之后,我注意到它只發生在很長的用戶名上。 無論如何,我實施了一個簡單的技巧來解決這個問題。
https://www.instagram.com/some_username/followers/
2.現在我創建了一個 function 來保存打開彈出窗口的代碼。 這將非常有用,因此將必要的代碼捕獲到 function 中。 (我沒有類名或 xpath,所以請為自己定制 function)
def openPopup():
self.browser.get(f"https://www.instagram.com/{self.username}")
global popup # we will need to access this variable outside of the function
popup = driver.find_element_by_class_name('popupClass') #you don't have to use class_name
popup.click()
while loop
在 Selenium 意外點擊用戶時不要掃描。 我們將使用第 1 步中的 URL。請確保在循環的頂部插入以下 if 語句,因此如果出現中斷,它將在嘗試訪問彈出窗口之前先處理它。 while True:
check_url = self.browser.current_url #returns string with current_url
if check_url != 'https://www.instagram.com/some_username/followers/':
#if this code is executed, this means there has been an accidental click
openPopup() #this will bring back to the page and reopen popup
#the rest of your code
popup.click() # variable from our function
action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(3)
newCount=len(dialog.find_elements_by_tag_name("li"))
if followerCount!=newCount or newCount==24:
print(f"New count:{newCount}")
time.sleep(3)
followerCount=newCount
else:
break
check_url = self.browser.current_url #we must recheck the current_url every time the loop runs to see if there has been a misclick
現在,每當您的循環檢測到 URL 不再是彈出窗口之一時,它會自動調用openPopup()
,這將使您返回頁面並返回彈出窗口,並且您的循環將繼續進行,就好像什么都沒發生一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.