Ruby 我正在使用SeleniumWebDriverGem来尝试点击facebook聊天栏,有时有效,有时无效

Ruby 我正在使用SeleniumWebDriverGem来尝试点击facebook聊天栏,有时有效,有时无效,ruby,facebook,selenium,selenium-webdriver,Ruby,Facebook,Selenium,Selenium Webdriver,我正在使用SeleniumWebDriverGem来尝试点击facebook聊天栏,有时有效,有时无效。当它不工作时,它返回Selenium元素not visible错误,但它清晰可见。我不确定我的代码出了什么问题 require 'selenium-webdriver' driver = Selenium::WebDriver.for :chrome # instantiates a google chrome session driver.navigate.to 'https://www.f

我正在使用SeleniumWebDriverGem来尝试点击facebook聊天栏,有时有效,有时无效。当它不工作时,它返回Selenium元素not visible错误,但它清晰可见。我不确定我的代码出了什么问题

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome # instantiates a google chrome session
driver.navigate.to 'https://www.facebook.com/' # takes you to facebook.com

emailBar = driver.find_element(:id,"email") #finds email input bar
passwordBar = driver.find_element(:id,"pass") #find password input bar
loginButton = driver.find_element(:id,"u_0_n") #finds login button

emailBar.send_keys "austinspreadsheet@gmail.com" # puts in the email
passwordBar.send_keys "YOURPASSWORD" # puts in the password
loginButton.click # clicks the login button

#THIS IS THE CODE BLOCK THAT DOES NOT WORK 
links = driver.find_elements(:class,"fbNubButton") # finds the chat bar
#driver.manage.timeouts.page_load = 10
links[0].click # opens the chat bar
links[1].click # NOTE that sometime it clicks and sometimes it doesn't but if you click both chat box classes it usually works, so the error is ok

我尝试过不同时点击两个聊天链接,这样做效果会降低。

我将Selenium与Python结合使用。在像您这样的情况下,这个问题与等待页面中的所有元素都被完全加载有关

Selenium的基本行为为您提供了显式和隐式等待。因此,基本上,您可以强制系统等待默认的秒数,或者等待元素加载

来自Selenium文档

显式等待

显式等待是您定义的代码,用于在继续执行代码之前等待特定条件发生。最糟糕的情况是Thread.sleep,它将条件设置为等待的确切时间段。提供了一些方便的方法,可以帮助您编写只在需要时等待的代码。WebDriverWait与ExpectedCondition相结合是实现这一目标的一种方法

require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://somedomain/url_that_delays_loading"

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
  element = wait.until { driver.find_element(:id => "some-dynamic-element") }
ensure
  driver.quit
end
隐式等待

隐式等待是告诉WebDriver在试图查找一个或多个元素(如果它们不立即可用)时轮询DOM一段时间。默认设置为0。设置后,将为WebDriver对象实例的生命周期设置隐式等待

require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10 # seconds

driver.get "http://somedomain/url_that_delays_loading"
element = driver.find_element(:id => "some-dynamic-element")

aberna在这个帖子上给你的答案有很多很好的信息,但它不会解决你的问题。如果使用aberna建议的显式等待方法,则可能还需要确保元素可见。单独使用.findElements并不能保证可点击性/可见性。您可以尝试使用expectedconditions.visibilityOfElementLocated,它还将检查可见性和存在性


或者,您也可以使用.findElement检查DOM上是否存在元素,但随后使用expectedconditions.visibilityOf检查元素的可见性部分。

我正在使用sleep5,然后再运行主逻辑。

我遇到了相同的问题。对我有效的解决方案是最大化我的浏览器窗口。这解决了许多失败的规范

Capybara.current_session.driver.browser.manage.window.maximize

我也面临这样的问题。我建议您在单击之前使用sleep 5。5秒就足够让DOM准备好了。嘿,伙计们,我刚刚使用了扩展页面功能使页面足够大,这样facebook好友就会自动出现在页面的侧面。driver.manage.window.resize_到1300940谢谢,我在代码的较新部分使用它,比如等待facebook好友加载。