Selenium - 无法单击导航栏下拉列表

Selenium - Unable to click on Navbar dropdown

提问人:Meeaad Bharoochi 提问时间:8/23/2023 最后编辑:James ZMeeaad Bharoochi 更新时间:8/25/2023 访问量:33

问:

我正在尝试运行一个脚本来自动执行某个任务。这是:

  1. Chrome 搜索网站
  2. 登录网站(填写详细信息)
  3. 导航到选项卡(称为“工作列表 V2”)
  4. 在“Jobs Listing V2”上输入产品条形码
  5. 捕获搜索返回的 XML 数据并将其保存到 XLS 文件
  6. 对下一个条形码重复上述步骤

我目前停留在第 3 步,无论我的代码是什么,它都会保留在网站的主页上。

到目前为止,我目前的代码是

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.maximize_window()

driver.get("https://brands.silo.co.za/#/Job/JobListingV2")

full_name_input = driver.find_element(By.ID, "Fullname")
full_name_input.send_keys("Meeaad Bharoochi")

full_name_input = driver.find_element(By.ID, "Password")
full_name_input.send_keys("Password@1")

submit_button = driver.find_element(By.CLASS_NAME, "btn-primary")
submit_button.click()

anchor_element = driver.find_element(By.LINK_TEXT, "Job Listing V2")
anchor_element.click()
<body>
    <div class="wrapper">
        
<nav class="navbar navbar-inverse navbar-fixed-top" style="height:auto !important; border:0;">
    <div class="container-fluid" style="margin: 0 !important">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#siloMenuItems" aria-expanded="false">
                <span class="sr-only"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#/Job/GetQAL">
                <img src="/Images/Silo_logo.png" style="width: auto; height: auto;" alt="">
            </a>
        </div>
        <div class="collapse navbar-collapse" id="siloMenuItems">
            <ul class="nav navbar-nav">
                                    <li class='active dropdown'>
                        <a href="#/SourceRequest/Index" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            Dashboard <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="#/SourceRequest/Index">Sourcing</a>
                            </li>
                            <li>
                                <a href="#/SalesRep/Dashboard">Sales Reps</a>
                            </li>
                            <li>
                                <a href="#/SalesRep/RequestsRequiringAttention">Product Line listings requiring attention</a>
                            </li>
                        </ul>
                    </li>
                                                                
                                    <li class="dropdown">
                        <a href="javascript:return;" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            Sourcing <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                            <li class="dropdown-submenu">
                                <a tabindex="-1" href="javascript:return;">Sourcing Statuses</a>
                                <ul id="_sourcingContext" class="dropdown-menu"></ul>
                            </li>
                            <li class="dropdown-submenu">
                                <a tabindex="-1" href="javascript:return;">Feeds</a>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a href="/#/Import/Index">Import Media Tracker Sourcing Requests</a>
                                    </li>
                                    <li>
                                        <a href="/#/ImportProductMaster/Index">Import Retailer Module Sourcing Requests</a>
                                    </li>
                                    <li>
                                        <a href="/#/Email/Logs">Email Logs</a>
                                    </li>
                                </ul>
                            </li>
                            <li class="dropdown-submenu">
                                <a tabindex="-1" href="javascript:return;">Reports</a>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a href="/#/Report/ImageStatusReport">Image Status Report</a>
                                    </li>
                                </ul>
                            </li>
                                <li>
                                    <a href="/#/SourceRequest/Create">Add sourcing request</a>
                                </li>
                            <li>
                                <a href="/#/SourceRequest/Index">Sourcing request listing</a>
                            </li>
                            <li>
                                <a href="/#/ProductRequest/Products">Product request Listing</a>
                            </li>
                            <li>
                                <a href="/#/SourceRequest/Companies">Company Overview</a>
                            </li>
                            <li>
                                <a href="/#/Ticket/Index">My Tickets</a>
                            </li>
                                                            <li class="dropdown-submenu">
                                    <a href="javascript:return;" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                                        Unique
                                    </a>
                                    <ul class="dropdown-menu">
                                        <li>
                                            <a href="#/Unique/Entry">Entries</a>
                                        </li>
                                        <li>
                                            <a href="#/Unique/Brand">Operational Off Pack Brands</a>
                                        </li>
                                    </ul>
                                </li>
                        </ul>
                    </li>
                                                    <li class="dropdown">
                        <a href="javascript:return;" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            Jobs <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            
                            <li>
**                                <a href="/#/Job/JobListingV2">Job Listing V2</a>
**

这是我需要自动化的网站的html.index。突出显示的是我无法选择的网页。

我尝试过 Xpaths,但我不确定到底出了什么问题,是等待吗?

jobs_dropdown = driver.find_element(By.XPATH, "//a[contains(@class, 'dropdown-toggle') and contains(text(), 'Jobs')]")

以及我尝试过

anchor_element = driver.find_element(By.LINK_TEXT, "Job Listing V2")
anchor_element.click()
selenium-webdriver 自动化 pycharm webdriverwait

评论

0赞 pcalkins 8/23/2023
你尝试过的东西应该有效......但也许最好以 href 为目标......类似于“//a[contains(@href, ”JobListingV2“)]的 XPATH 我有一种感觉,缺少 webdriverwaits 可能是这里的问题......听起来您还需要先单击某些内容才能显示链接?“Jobs”与插入符号的链接?您还应该避免通过类名查找元素,因为这些元素不能保证是唯一的。例如:submit_button = driver.find_element(By.CLASS_NAME, “btn-primary”)
1赞 Meeaad Bharoochi 8/25/2023
嗨@pcalkins是的,你是 100% 正确的。就“工作”插入符号而言,它仅在单击导航栏标题后才会出现。我添加了长达 80 秒的 WebDriverwaits。但它仍然支持一个错误。我相信工作链接是你说的问题。我试图弄清楚这一切,因为这也是我第一次使用 Python。竖起大拇指,我做对了!

答:

0赞 Meeaad Bharoochi 8/25/2023 #1

再次感谢@pcalkins我添加了 webdriverwait,它为我的脚本创造了奇迹。

所做的调整是

dropdown_toggle = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, 'dropdown-toggle')))