简体   繁体   中英

Executing a script in selenium python

I am trying to execute this script in selenium.

<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>

This is my code to execute it.

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

It didn't work. What am i doing wrong?

Find the element with selenium and pass it to execute_script() to click:

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)

Since I know the context of the problem, here is the set of things for you to do to solve it:

  • click on the "11 others" link via javascript relying on the solution provided here: How to simulate a click with JavaScript?
  • make a custom expected condition to wait for the element text not to ends with "11 others like this." text (this is a solution to the problem you had at 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 

Implementation:

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()

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