我正在尝试使用硒python测试网页。一切正常,但在单击导航栏项
时遇到问题我用过:
driver.find_element_by_xpath('./li/a[. = "Log in"]')
还使用过:
driver.find_element_by_link_text('Log in')
没有运气!
代码段:
<div class='container'>
<div class='navigationbar__header'>
<a class='navigationbar__header__logo tracking-link' data-link-name='logo' href='/' target='_self'>
<div id='hired-brand'>HIRED</div>
</a>
</div>
<div class='navigationbar__toggle'>
<div class='navigationbar__toggle__element'>
<img alt='Menu' class='icon icon--sandwich' src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'>
</div>
<input class='navigationbar__toggle__helper' type='checkbox'>
<ul class='navigationbar__navigation'>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_page" target="_self" href="/employers">For Employers</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="success_stories" target="_self" href="/success-stories">Success Stories</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_resources" target="_self" href="/employers/resources">Resources</a></li>
<li class="navigationbar__item "><a class="text-medium sm-ml0 tracking-link" data-link-name="login" target="_self" href="/login">Log in</a></li>
<div class='xs-ptb1 xs-prl1 md-ptb0 md-inline-block'><li class="navigationbar__item "><a class="button button--primary tracking-link" data-link-name="signup" target="_self" href="/signup">Sign Up</a></li></div>
</ul>
</div>
</div>
</nav>
此代码在页面上可见,易于检查。有谁知道与之互动的更好方法?
答案 0 :(得分:1)
在website内以文本为登录的链接上的click()
,您需要为element_to_be_clickable()
引入 WebDriverWait 并且您可以使用以下任一Locator Strategies:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='navigationbar__item ']/a[@data-link-name='login' and @href='/login']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您也可以按以下方式使用execute_script()
:
使用CSS_SELECTOR
:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))))
使用XPATH
:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='navigationbar__item ']/a[@data-link-name='login' and @href='/login']"))))
答案 1 :(得分:0)
好,所以我检查了site。
问题是,当窗口较小时,需要首先单击导航切换按钮。
尝试
from selenium.common.exceptions import NoSuchElementException
try:
login_button = driver.find_element_by_link_text('Log in')
login_button.click()
except NoSuchElementException:
nav_bar_toggle = driver.find_element_by_class_name(
'navigationbar__toggle__helper'
)
nav_bar_toggle.click()
login_button = driver.find_element_by_link_text('Log in')
login_button.click()