Reputation: 137
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!
Upvotes: 0
Views: 477
Reputation: 346
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.
Upvotes: 1