我正在尝试对服务器具有多个访问者的功能进行一些测试,并确保我可以处理流量,因此我选择使用硒和python,因为我对它们最熟悉
这是我到目前为止(正在进行的工作):
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.on-demandlogistics.com/home.html")
element = WebDriverWait(driver, 10).until(
ec.element_to_be_clickable((By.CLASS_NAME, "test"))
)
element.click()
我尝试了CSS选择器,Xpath,甚至在HTML代码中创建了特殊的类,但是我无法让硒跟随我想要的链接
我要登录,然后从那里继续
HTML如下,但可以随意点击链接(http://www.on-demandlogistics.com/home.html)
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<head>
<meta charset="utf-8">
<meta name="description" content="This is the description">
<link rel="stylesheet" href="style.css">
<title>On-Demand Logistics | Home</title>
</head>
<header>
<nav class="nav main-nav">
<div class="nav-grid-container">
<div class="nav-grid-item link-img">
<a href="mailto:slefebvre@on-demandlogistics.com">
<img src="images/headerlogo.png" alt="">
</a>
</div>
<div class="nav-grid-item">
<a href="home.html">Home</a>
</div>
<div class="nav-grid-item">
<a href="services.html">Services</a>
</div>
<div class="nav-grid-item">
<a href="aboutus.html">About Us</a>
</div>
<div class="nav-grid-item">
<a href="contactus.html">Contact Us</a>
</div>
<div class="nav-grid-item test">
<a href="login.html">Log-In</a>
</div>
<div class="nav-grid-item right-imgs">
<img src="images/footerlogo.png" alt="">
</div>
</div>
</nav>
</header>
<section class="main-section">
<h1>Welcome to On-Demand Logistics!</h1>
<div class="grid-container-even">
<div class="grid-item grid-item-1">
<h2>
We are a 3PL (third party logistics) and consulting company
</h2>
</div>
<div class="grid-item grid-item-2">
<img src="images/openmap.jpg" width=400px hieght=1000px alt="This is the open globe image">
</div>
<div class="grid-item grid-item-3">
...
任何帮助将不胜感激
谢谢!
littlejiver
答案 0 :(得分:1)
您选择的元素是div
。您要单击其中的锚标记-
element = WebDriverWait(driver, 10).until(
ec.element_to_be_clickable((By.CSS_SELECTOR, ".test > a"))
)
element.click()
css选择器.test > a
仅表示选择a
标签,它是类名称为test
的元素的子元素。
有时,硒无法注册对嵌套元素的点击。这里似乎是这种情况。在这种情况下,您可以像这样从javascript中启动click
-
driver.execute_script("arguments[0].click()", element)
或者您可以简单地从href
标记中获取a
并手动执行.get
-
driver.get(element.get_attribute('href'))