0

I'm new to Python and Selenium, and I just ran into a problem. I'm trying to make a script that will automatically click on the LIKE button of a Facebook page (in a pop-up).

EDIT - Localhost Webpage code:

<html>
<head>
<title>Link to Facebook Page</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<center>
<div id="fb_like">
<a href="https://m.facebook.com/AdagioTV" target="_blank"><img src="fblogo.png"></a>
</div>
</center>
</body>
</html>

I have the following code:

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

webpage = r"http://localhost/like/" # local web

driver = webdriver.Firefox()
driver.get(webpage)

sbox = driver.find_element_by_class_name("fb_like")
sbox.click()

print "opened fb popup"

Now, the page opens the mobile version of our facebook page (IN A POP-UP), which is something like: https://m.facebook.com/AdagioTV

I have the following code in order to wait for the page to load and click the like button:

time.sleep(5)
print "slept 5 seconds" # page loading time
WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((By.ID, 'action_bar')))
likebutt = driver.find_element_by_link_text('Like')
likebutt.click()

However, this will not FIND the elements, neither Like or action_bar, and it will ever-sleep. What am I doing wrong here? Thanks!

unkn0wnx
  • 137
  • 3
  • 14

1 Answers1

1

Try:

String parentWindowHandler = driver.getWindowHandle(); //parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window

time.sleep(5)
WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((By.ID, 'action_bar')))
likebutt = driver.find_element_by_link_text('Like')
likebutt.click()

by the way if it didn't work, try wait for another element locator in popup (more easy), it's may be problem with wrong locator: I not see "action bar" in your Webpage code.

V. Rob
  • 316
  • 1
  • 4
  • 13
  • Thanks a lot! Your code was different but you gave me a good idea, about using Window Handles. After a bit of research, I found out that " driverfb.switch_to.window(driverfb.window_handles[1]) " does the job. Thanks a lot again! Your code was really really helpful. Btw, your code is for java? I'm using python. – unkn0wnx Aug 22 '17 at 12:10
  • yep, java. I just wanted to show very same workaround from my test. Anyway, you are welcome. – V. Rob Aug 22 '17 at 12:51
  • r u post your answer on py? – V. Rob Aug 22 '17 at 12:51
  • Yes, the line is in python. Anyway, if you didn't say anything about window handles, I'd be probably stuck for a looong time with it, as I am new to Selenium and Py, and wouldn`t know what to search for! – unkn0wnx Aug 22 '17 at 13:25