【问题标题】:Selenium PhantomJS webdriver doesn't clickSelenium PhantomJS webdriver 没有点击
【发布时间】:2016-08-06 09:06:34
【问题描述】:

此代码使用 firefox webdriver 完美运行。但是对于 PhantomJS,它不会单击所需的 javascript 链接。

driver = webdriver.PhantomJS()
driver.get("http://justbet.co.ke/index.php?option=com_justbet&league=1539&Itemid=123")
options = driver.find_elements_by_xpath("//td[@class='optionmore']")
for more in range(0, len(options)):
        options[more].click()
        sleep(3)

【问题讨论】:

  • 如果我比较两个结果的页面源,它​​们是不同的。使用 firefox,源包含通过单击 javascript 链接激活的部分,而使用 phantomjs 则不包含

标签: python python-2.7 selenium phantomjs


【解决方案1】:

如果您在 optionmore 类中选择 a 标签,该代码将起作用:

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get("http://justbet.co.ke/index.php?option=com_justbet&league=1539&Itemid=123")
options = driver.find_elements_by_xpath("//td[@class='optionmore']/a")
print(driver.find_elements_by_xpath("//td[@class='suboption ']"))
for opt in options:
    opt.click()
print(driver.find_elements_by_xpath("//td[@class='suboption ']"))

运行上面的代码:

In [27]: from selenium import webdriver

In [28]: driver = webdriver.PhantomJS()    
In [29]: driver.get("http://justbet.co.ke/index.php?option=com_justbet&league=1539&Itemid=123")    
In [30]: options = driver.find_elements_by_xpath("//td[@class='optionmore']/a")    
In [31]: print(len(driver.find_elements_by_xpath("//td[@class='suboption ']")))
0

In [32]: for opt in options:
   ....:         opt.click()
   ....:     

In [33]: print(len(driver.find_elements_by_xpath("//td[@class='suboption ']")))
270

你得到所有你想要的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多