2015-03-31 306 views
2

我想在硒中執行這個腳本。在python中執行腳本

<div class="vbseo_liked"> 
<a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> 
, 
<a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollow">Sikonge</a> 
, 
<a href="http://www.jamiiforums.com/member.php?u=8979" rel="nofollow">Ab-Titchaz</a> 
and 
<a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a> 
like this. 
</div> 

這是我的代碼來執行它。

browser.execute_script("document.getElement(By.xpath(\"//div[@class='vbseo_liked']/a[contains(@onclick, 'return vbseoui.others_click(this)')]\").click()") 

它沒有工作。我究竟做錯了什麼?

回答

2

找到硒元素,並把它傳遞給execute_script()點擊:

link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]') 
browser.execute_script('arguments[0].click();', link) 

因爲我知道這個問題的背景下,這裏是你如何解決它的一套東西:

  • 點擊 「11人」 通過JavaScript依託解決方案鏈接此處提供:How to simulate a click with JavaScript?
  • 使custom expected condition等待元素文本不以「11個這樣的其他人」結束。文本(這是你必須在Expected conditions with selenium該問題的解決方案):

    class wait_for_text_not_to_end_with(object): 
        def __init__(self, locator, text): 
         self.locator = locator 
         self.text = text 
    
        def __call__(self, driver): 
         try : 
          element_text = EC._find_element(driver, self.locator).text.strip() 
          return not element_text.endswith(self.text) 
         except StaleElementReferenceException: 
          return False 
    

實現:

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


class wait_for_text_not_to_end_with(object): 
    def __init__(self, locator, text): 
     self.locator = locator 
     self.text = text 

    def __call__(self, driver): 
     try : 
      element_text = EC._find_element(driver, self.locator).text.strip() 
      return not element_text.endswith(self.text) 
     except StaleElementReferenceException: 
      return False 


browser = webdriver.PhantomJS() 
browser.maximize_window() 
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html") 

username = browser.find_element_by_id("navbar_username") 
password = browser.find_element_by_name("vb_login_password_hint") 

username.send_keys("MarioP") 
password.send_keys("codeswitching") 

browser.find_element_by_class_name("loginbutton").click() 

wait = WebDriverWait(browser, 30) 
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]'))) 
wait.until(EC.title_contains('Kenyan & Tanzanian')) 
wait.until(EC.visibility_of_element_located((By.ID, 'postlist'))) 

# click "11 others" link 
link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]') 
link.click() 
browser.execute_script(""" 
function eventFire(el, etype){ 
    if (el.fireEvent) { 
    el.fireEvent('on' + etype); 
    } else { 
    var evObj = document.createEvent('Events'); 
    evObj.initEvent(etype, true, false); 
    el.dispatchEvent(evObj); 
    } 
} 

eventFire(arguments[0], "click"); 
""", link) 

# wait for the "div" not to end with "11 others link this." 
wait.until(wait_for_text_not_to_end_with((By.CLASS_NAME, 'vbseo_liked'), "11 others like this.")) 

print 'success!!' 
browser.close() 
+0

selenium.common.exceptions.WebDriverException:消息:{ 「的errorMessage」:「」 undefined「不是函數(評估'arguments [0] .click()')」, – user3078335 2015-03-31 20:41:59

+0

這是它給我的錯誤信息。不知道是什麼原因。 – user3078335 2015-03-31 20:43:23

+0

@ user3078335是啊,此刻與它戰鬥,嘗試不同的方法.. – alecxe 2015-03-31 20:43:32