【发布时间】:2019-03-19 21:05:22
【问题描述】:
我正在尝试在 Python 中使用 Selenium 抓取一个机器人顾问网站。我遇到的问题是,尽管尝试使用 CSS 选择器和 XPATH 进行选择,但我无法选择并单击元素来回答所需的问题。我认为这里发生了一些奇怪的事情,我错过了所以任何帮助将不胜感激。
下面的代码 sn-p 显示了如何获取调查并尝试单击时间范围滑块中的第一个答案。不幸的是,这会引发错误:selenium.common.exceptions.NoSuchElementException: Message: no such element.
对不起,该网站是德语的,但如果您有任何问题,请告诉我!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
# Load Selenium Webdriver
driver = webdriver.Chrome(r'C:\Users\jcgou\PycharmProjects\Master Thesis Robo Advisors\Chrome Driver\chromedriver.exe')
# Navigate to Fintego
fintegoUrl = 'https://www.fintego.de/'
driver.set_page_load_timeout(10)
driver.get(fintegoUrl)
driver.maximize_window()
# Navigate to portfolio recommendation survey
driver.find_element_by_link_text('Depot eröffnen').click()
sleep(3)
# Time Horizon
driver.find_element_by_xpath("//ul[@id='eox_ContentPane_3_Layout_AnlagehorizontBlock_lstAnlagehorizont']//li[2]//i[1]").click()
如果有帮助的话,这里是 HTML。我正在尝试选择并点击“1 - 3 Jahre”。
<ul class="form name m-deo-form marginBottomDouble">
<li id="eox_ContentPane_3_Layout_AnlagehorizontBlock" class="marginBottom">
<label for="eox_ContentPane_3_Layout_AnlagehorizontBlock_lstAnlagehorizont" class="left selectList-label">
<span id="eox_ContentPane_3_Layout_AnlagehorizontBlock_RequiredfieldvalidatorErfahrung" title="Bitte treffen Sie eine Auswahl" class="validator icon-exclamation" style="color:Red;visibility:hidden;"></span>
</label>
<div class="right">
<ul id="eox_ContentPane_3_Layout_AnlagehorizontBlock_lstAnlagehorizont" class="selectList">
<li class="item stopLightboxSehrKurzfristig" data-value="1" style="width:25%;"><span class="text">Kürzer als 1 Jahr</span><i></i></li>
<li class="item" data-value="2" style="width:25%;"><span class="text">1 - 3 Jahre</span><i></i></li>
<li class="item" data-value="3" style="width:25%;"><span class="text">3 - 7 Jahre</span><i></i></li>
<li class="item" data-value="4" style="width:25%;"><span class="text">Länger als 7 Jahre</span><i></i></li>
</ul><input type="hidden" name="eox_ContentPane_3$Layout$AnlagehorizontBlock$lstAnlagehorizont_ValueContainer" id="eox_ContentPane_3_Layout_AnlagehorizontBlock_lstAnlagehorizont_ValueContainer">
</div>
</li>
</ul>
【问题讨论】:
-
“这不起作用。” 是什么意思?它点击错误的东西吗?它是否单击正确但没有任何反应?它是否抛出错误,如果是,错误是什么?
-
对不起!它抛出错误:selenium.common.exceptions.NoSuchElementException: Message: no such element
标签: python google-chrome selenium-webdriver xpath ui-automation