我正在尝试运行一个自动执行特定任务的脚本。这是:
我目前陷入了第 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()
再次感谢@pcalkins,我添加了 webdriverwait,它为我的脚本创造了奇迹。
所做的调整是
dropdown_toggle = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, 'dropdown-toggle')))