1

I am confused. When I right click on the target web and visit the html source code, the options are all listed:

<div class="float_typeid">
  <select name="typeid" id="typeid">
    <option value="0">分类</option><option value="90">弈剑出售</option><option value="89">冰心出售</option><option value="88">太虚出售</option><option value="87">云麓出售</option><option value="86">魍魉出售</option><option value="85">翎羽出售</option><option value="84">天机出售</option><option value="83">荒火出售</option><option value="97">弈剑购买</option><option value="98">冰心购买</option><option value="99">太虚购买</option><option value="100">云麓购买</option><option value="101">魍魉购买</option><option value="102">翎羽购买</option><option value="103">天机购买</option><option value="104">荒火购买</option><option value="120">鬼墨购买</option><option value="121">鬼墨出售</option><option value="122">龙巫出售</option><option value="123">龙巫购买</option>
  </select>
</div>

But when I visit the web in developers' tool (F12), it was:

<div class="float_typeid">
  <select name="typeid" id="typeid">
    <option value="0"></option>
  </select>
  <a href="javascript:;" hidefocus="true" id="typeid_ctrl" tabindex="1">分类</a>
</div>

When I tries to set option via selenium.webdriver.support.select.Select, it fails. From the error information I guess it is the same situation as developers' tool -- only one option is grabbed which value is 0:

import requests
import json
import re
from os.path import dirname, realpath
from time import sleep
from selenium.webdriver.support import select
from selenium import webdriver

USERNAME = "smaller9@163.com"
PASSWORD = "111111"
loginUrl = "http://tx3.netease.com/logging.php?action=login"
postUrl = "http://tx3.netease.com/post.php?action=newthread&fid=153&extra="
# create a driver
driver = webdriver.Firefox()
# get the homepage
driver.get(loginUrl)

un_elm = driver.find_element_by_id("username")
pw_elm = driver.find_element_by_id("ori_password")
submit = driver.find_element_by_css_selector("[name=loginsubmit]")

un_elm.send_keys(USERNAME)
pw_elm.send_keys(PASSWORD)

# click submit
submit.click()

# ----------above is all OK. below begins to make a post---------
def post(postUrl, title, content):
    driver.get(postUrl)
    tt_elm = driver.find_element_by_id("subject")
    mg_elm = driver.find_element_by_id("e_textarea")
    ctg_elm = driver.find_element_by_id("typeid")
    submit = driver.find_element_by_css_selector("[name=topicsubmit]")
    tt_elm.send_keys(title)
    mg_elm.send_keys(content)
    ctg_obj = select.Select(ctg_elm)
    print(ctg_obj.all_selected_options) # only one option
    ctg_obj.select_by_visible_text('冰心出售') 
    # ctg_obj.select_by_value('89') also doesn't work.
    submit.click()
    return 1

post(postUrl,
     "80神启v9粉翅膀战场套沙包BX完封7物理防护22知彼640熊双太阴灵化马大禹天域硬件全",
     "http://tx3.netease.com/viewthread.php?tid=3891487")

Result:

>>> 
[<selenium.webdriver.remote.webelement.WebElement object at 0x031F9B90>]
Traceback (most recent call last):
  File "C:\Users\T430i\Desktop\xxx.py", line 60, in <module>
    "http://tx3.netease.com/viewthread.php?tid=3891487")
  File "C:\Users\T430i\Desktop\xxx.py", line 54, in post
    ctg_obj.select_by_visible_text('冰心出售')
  File "C:\Python34\lib\site-packages\selenium\webdriver\support\select.py", line 136, in select_by_visible_text
    raise NoSuchElementException("Could not locate element with visible text: %s" % text)
selenium.common.exceptions.NoSuchElementException: Message: 'Could not locate element with visible text: 冰心出售' 
xiang
  • 393
  • 3
  • 14

1 Answers1

0

It would seem that there is an AJAX call changing the HTML after the DOM has loaded. The solution would be to wait for the AJAX to complete. I had previously done stuff like this just by adding a time.sleep(10) before trying to parse the HTML, but this question has some discussion about how to do this the proper way. You can also try:

waitForElementPresent(locator)

To wait for the element to be present in the DOM.

Good luck

Community
  • 1
  • 1
WeaselFox
  • 7,220
  • 8
  • 44
  • 75
  • I add a `time.sleep(10)` or `driver.implicitly_wait(10)` after `driver.get(postUrl)`, but both still raise error as before. I've checked documentation of selenium, there seems no API for `waitForElementPresent`. – xiang Aug 31 '14 at 08:36