我想单击网页左侧导航中的<li>项,使用Python和Selenium webdriver来定位I

2024-09-29 21:52:13 发布

您现在位置:Python中文网/ 问答频道 /正文

I want to click on MAKE UP in the left navigation, Please find attached image and link for the webpage

Image for the Webpage

Link for the Webpage

I am currently using the below code to click on the item but not getting any result.I am able to acces the elements by class name('has-sub').I can even print them but cant click them

obc = driver.find_elements_by_class_name('has-sub')
for ea in obc:
   if ea.text == "Makeup":
    ea.click()

下面的更多信息是网页的html代码

<li class="has-sub" style="height: 38px;">
    <a href="#">Makeup</a>  
    <ul class="submenu" style="top: 0px;">

        <li>
            <a id="SBN_facet_Face" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/face" escapexml="false">Face </a>
        </li>

        <li>
            <a id="SBN_facet_Lips" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/lips" escapexml="false">Lips </a>
        </li>

        <li>
            <a id="SBN_facet_Eyes" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/eyes" escapexml="false">Eyes </a>
        </li>

        <li>
            <a id="SBN_facet_Nails" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/nails" escapexml="false">Nails </a>
        </li>

        <li>
            <a id="SBN_facet_Brushes &amp; Tools" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/beauty-brushes-accessories" escapexml="false">Brushes &amp; Tools </a>
        </li>

        <li>
            <a id="SBN_facet_Makeup" href="http://shop.davidjones.com.au/djs/en/davidjones/beauty/beauty-makeup" escapexml="false">All Makeup </a>
        </li>

    </ul>
</li>`enter code here`

任何帮助都将不胜感激


Tags: thecomidhttplishopfaceten
3条回答

这里的问题是,您试图在文本位于元素下时单击该元素。所以你需要做的是:

obc = driver.find_elements_by_xpath('//li[@class='has-sub']/a[contains(text(), 'Makeup')]')

我在你的网页上测试了xpath,它成功了

你知道吗

根据您提供的HTML,要单击左侧导航窗格中的MAKE-UP,可以使用以下代码块:

obc = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='aside all-open']/ul//li[@class='has-sub']/a")))
for ea in obc:
    if 'Makeup' in ea.get_attribute("innerHTML"):
        ea.click()
        break

我可以点击使用下面的代码

wait = WebDriverWait(driver, 10)
elements  = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//li[@class='has-sub']")))
for element in elements:
    if element.find_elements_by_link_text("Makeup"):
        element.click()
        break
innerElements  = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//li[@class='has-sub open']/ul/li")))
for innerElement in innerElements:
    if innerElement.text == "Face":
        innerElement.click()
        break

希望这对你有帮助

相关问题 更多 >

    热门问题