简体   繁体   中英

Python bindings to Selenium Webdriver: ActionChain not executing in PhantomJS

Using PhantomJS, the ActionChain shown below never executes:

import sys

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# have PhantomJS pretend to be Firefox to make things more equal
desired_capabilities = dict(DesiredCapabilities.PHANTOMJS)
desired_capabilities["phantomjs.page.settings.userAgent"] = (
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) '
    'Gecko/20100101 Firefox/21.0'
)
wd = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
# wd = webdriver.Firefox()

# get a page with some AJAXy comments
wd.get('http://www.cartoonbrew.com/disney/disneys-princess-makeover-of-merida-leads-to-uproar-and-petition-82636.html')

# switch to iframe with comments
wd.switch_to_frame('dsq1')

# these selectors are used throughout
load_more_div_selector = "//div[@class='load-more']"
load_more_button_selector = "//div[@class='load-more']/a"

# wait until the comments are loaded
WebDriverWait(wd, 10, 0.1).until(
    EC.presence_of_element_located((By.XPATH, load_more_div_selector))
)

# print out the status of the load more button (the display is 'block'
# if there are more comments to be loaded), and the button should have
# the class 'btn'
load_more_div = wd.find_element_by_xpath(load_more_div_selector)
load_more_button = wd.find_element_by_xpath(load_more_button_selector)
print >> sys.stderr, '(%s, %s)' % (load_more_div.value_of_css_property('display'), load_more_button.get_attribute('class'))

# click on the 'load more comments' button
action_chain = ActionChains(wd).click(on_element=load_more_button)
action_chain.perform()

# wait until the comments are loaded
def comments_no_longer_loading(wd):
    load_more_div = wd.find_element_by_xpath(load_more_div_selector)
    load_more_button = wd.find_element_by_xpath(load_more_button_selector)
    is_loading = 'busy' in load_more_button.get_attribute('class')
    if is_loading:
        print >> sys.stderr, 'comments are loading, (%s, %s)' % \
            (load_more_div.value_of_css_property('display'),
             load_more_button.get_attribute('class'))
    else:
        print >> sys.stderr, 'comments done loading (%s, %s)' % \
            (load_more_div.value_of_css_property('display'),
             load_more_button.get_attribute('class'))
    return not is_loading

# wait until comments are loaded
WebDriverWait(wd, 10, 0.1).until(comments_no_longer_loading)

# print out the status of the button
load_more_div = wd.find_element_by_xpath(load_more_div_selector)
load_more_button = wd.find_element_by_xpath(load_more_button_selector)
print >> sys.stderr, '(%s, %s)' % (load_more_div.value_of_css_property('display'), load_more_button.get_attribute('class'))

print wd.page_source

wd.quit()

Running using Firefox prints this output to stderr, which is correct:

(block, btn)
comments are loading, (block, btn busy)
comments are loading, (block, btn busy)
comments done loading (none, btn)
(none, btn)

But running using PhantomJS prints this output to stderr, which is incorrect:

(block, btn)
comments done loading (block, btn)
(block, btn)

I would like to be able to use the ActionChain version for the application that I'm writing, but also need to use PhantomJS. Any ideas why the ActionChain does not execute using the PhantomJS webdriver?

I can't tell what's wrong with your real site, but for http://example.com I believe that's just timing issue. Add time.sleep(5) before print wd.current_url , you should be able to get the correct url.

import time

# your code
# ...

ActionChains(wd).click(on_element=button).perform() 
time.sleep(5)
print wd.current_url

(Remove sleep and use WebDriverWait if you want to do some real stuff.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM