我很困惑。当我右键单击目标Web并访问html源代码时,所有选项都列出:
<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>
但是当我在开发者网站上访问网页时工具(F12),它是:
<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>
当我尝试通过selenium.webdriver.support.select.Select
设置选项时,它会失败。根据错误信息,我猜这与开发人员的情况相同。工具 - 只抓取一个选项,其值为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")
结果:
>>>
[<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: 冰心出售'
答案 0 :(得分:0)
在DOM加载后,似乎有一个AJAX调用改变了HTML。解决方案是等待AJAX完成。我以前只是在尝试解析HTML之前添加time.sleep(10)
来完成这样的事情,但是this问题讨论了如何以正确的方式执行此操作。您也可以尝试:
waitForElementPresent(locator)
等待元素出现在DOM中。
祝你好运