[英]Selenium find element and click on it
我试图让Selenium单击“ View All Companies
按钮,但是我不确定自己在做什么错。 它no element found
返回no element found
HTML代码
<div class="screener-toggles">
<div class="buttons">
<span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span>
<span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span>
<span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span>
</div>
</div>
我写的python代码
element1 = driver.find_elements_by_class_name('View All Companies')
element1.click()
# I have tried all-companies instead of View All Companies as well. But still doesn't work
我应该不使用find_elements_by_class_name
吗?
任何关于我做错事的建议都将不胜感激!
试试xpath : "//span[contains(text(),'View All Companies')]"
是的,您不应使用find_elements_by_class_name
而应使用find_element_by_class_name
。
当期望定位器返回多个元素时,将使用find_elements_by_class_name
。 对于特定元素,仅使用find_element_by_class_name
。
另一件事是,我无法在您的HTML代码中看到任何类名称为“ View All Companies
。 请查看您的HTML并仔细选择类名或其他定位符
希望对您有帮助
View All Companies
是文字,不是课程。 尝试使用css_selector
或xpath
按文本css_selector
element1 = find_element_by_css_selector('span:contains("View All Companies")')
element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]')
或通过包含all-companies
的data-name
属性
element1 = find_element_by_css_selector('span[data-name*="all-companies"]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.