簡體   English   中英

Python Selenium-無法使用XPATH單擊滑塊元素

[英]Python Selenium - Can't click on slider elements using XPATH

我正在嘗試使用Python中的Selenium抓取一個機器人顧問網站。 我遇到的問題是盡管嘗試使用CSS選擇器和XPATH進行選擇,但我無法選擇並單擊元素來回答所需的問題。 我認為這里缺少一些奇怪的信息,因此我們將不勝感激。

下面的代碼段顯示了如何獲取調查並嘗試單擊時間范圍滑塊中的第一個答案。 不幸的是,這引發了錯誤: 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>

試試這個,它在Chrome中對我有用。 您可能需要用顯式等待替換可怕的time.sleep實現。 我將切換到新窗口以查找要單擊的元素(可能這就是為什么您的代碼無法正常工作的原因)

from selenium.webdriver import Chrome
import time

driver = Chrome()
driver.get('https://www.fintego.de/')

# Navigate to portfolio recommendation survey
driver.find_element_by_link_text('Depot eröffnen').click()
time.sleep(10)

# Switch to new window
driver.switch_to.window(driver.window_handles[1])
anlage_horizon =  driver.find_element_by_id(
                    'eox_ContentPane_3_Layout_AnlagehorizontBlock_lstAnlagehorizont'
                  )

for item in anlage_horizon.find_elements_by_class_name('item'):
    if item.text == '1 - 3 Jahre':
        item.click()

只需轉到抓取元素並單擊它,

driver.find_element_by_xpath("*//span[text()='1 - 3 Jahre']").click();

只有在您的HTML DOM中可點擊時,此方法才有效(我希望是這樣)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM