【问题标题】:Python Selenium - Can't click on slider elements using XPATHPython Selenium - 无法使用 XPATH 单击滑块元素
【发布时间】: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


【解决方案1】:

试试这个,它在 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()

【讨论】:

  • 哇,非常感谢这对我有用!我肯定会在刮板的其余部分使用这个逻辑。我想知道为什么你必须切换到一个新窗口才能让它工作......
  • 很高兴它成功了。该页面在新窗口中打开,据我所知,您需要显式切换到新窗口才能在新打开的页面上工作
【解决方案2】:

只需去抓取元素并点击它,

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

只有在您的 HTML DOM 中可点击时才有效(我希望如此)。

【讨论】:

  • 不幸的是,我确实尝试过这样做。它不起作用并抛出上面 cmets 中提到的错误。
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多