到Selenium Webdriver的Python绑定:ActionChain未在PhantomJS中执行

到Selenium Webdriver的Python绑定:ActionChain未在PhantomJS中执行,python,selenium,webdriver,phantomjs,Python,Selenium,Webdriver,Phantomjs,使用PhantomJS,下面显示的ActionChain永远不会执行: 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.com

使用PhantomJS,下面显示的ActionChain永远不会执行:

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()
使用Firefox运行会将此输出打印到stderr,这是正确的:

(block, btn)
comments are loading, (block, btn busy)
comments are loading, (block, btn busy)
comments done loading (none, btn)
(none, btn)
但使用PhantomJS运行会将此输出打印到stderr,这是不正确的:

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

我希望能够为我正在编写的应用程序使用ActionChain版本,但也需要使用PhantomJS。你知道为什么ActionChain不使用PhantomJS webdriver执行吗?

我不知道你的真实站点有什么问题,但我想知道
http://example.com
我认为这只是时间问题。添加
time.sleep(5)
print wd.current\u url
之前,您应该能够获得正确的url

import time

# your code
# ...

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

(如果你想做一些真正的事情,请删除睡眠并使用WebDriverWait。)

我不知道你真正的网站有什么问题,但对于
http://example.com
我认为这只是时间问题。添加
time.sleep(5)
print wd.current\u url
之前,您应该能够获得正确的url

import time

# your code
# ...

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

(如果你想做一些真正的事情,请取消睡眠并使用WebDriverWait。)

我觉得逻辑是正确的。如果这是你能提供的,我们将无法复制任何东西并提供帮助。请找到一个可公开访问的站点,您可以复制该问题。@user1177636感谢您的回复!我刚刚编辑了这个问题,所以代码是一个完整的例子,可以重现这个问题。如果这是你能提供的,我们将无法复制任何东西并提供帮助。请找到一个可公开访问的站点,您可以复制该问题。@user1177636感谢您的回复!我刚刚编辑了这个问题,所以代码是一个完整的例子,可以重现这个问题。再次感谢您的回复!我用一个更真实的代码示例再次编辑了这个问题。WebDriverWait确实解决了我之前发布的简化示例的问题。(我很乐意接受这个答案,但如果你能帮我举一个现实的例子,我会非常感激!)我很感激你的帮助,但我还是不明白为什么Firefox和PhantomJS之间有区别再次感谢你的回答!我用一个更真实的代码示例再次编辑了这个问题。WebDriverWait确实解决了我之前发布的简化示例的问题。(我很乐意接受这个答案,但如果你能帮我做一个现实的例子,我会非常感激!)我很感激你的帮助,但我还是不明白为什么Firefox和PhantomJS之间有区别