[英]Find element by xpath and click on it
I am trying to load complete list of images on a page. 我正在尝试在页面上加载完整的图像列表。 There is a "Show More" button on the page.
页面上有一个“显示更多”按钮。 I want to click it until all the images are loaded.
我想点击它直到加载所有图像。
HTML: HTML:
<li class="show-more-row">
<button class="show-more-cta bottom-btn ga"data-ga-action="click"data-ga-label="category-see-more:SectionCourses>Show More
<i class="lyndacon arrow-down"></i>
</button>
</li>
I have to tried to get the element by Xpath. 我必须尝试通过Xpath获取元素。 But it keeps on changing every time I click on the button.
但每次点击按钮时它都会不断变化。
Example: 例:
//*[@id="category-courses"]/li[101]/button
and //*[@id="category-courses"]/li[151]/button
//*[@id="category-courses"]/li[101]/button
and //*[@id="category-courses"]/li[151]/button
I have also tried finding element by class name: 我也尝试按类名查找元素:
while(browser.find_element_by_class_name("show-more-cta bottom-btn ga")):
moreElem = browser.find_element_by_class_name("show-more-cta bottom-btn ga")
moreElem.click()
But it gives error: 但它给出了错误:
invalid selector: Compound class names not permitted
无效的选择器:不允许使用复合类名
Compound/multiple classes cannot be used in a "by class name" locator . 复合/多个类不能在“按类名”定位器中使用 。 You have to either leave a single class in the locator:
您必须在定位器中保留一个类:
browser.find_element_by_class_name("show-more-cta")
Or, use a different strategy, eg a CSS selector: 或者,使用不同的策略,例如CSS选择器:
browser.find_element_by_css_selector(".show-more-cta.bottom-btn.ga")
Note that I'd just leave a single "show-more-cta" class - it is a good choice since it brings value and meaning to the locator making it readable. 请注意,我只留下一个“show-more-cta”类 - 它是一个很好的选择,因为它为定位器带来了价值和意义,使其可读。
bottom-btn
and ga
classes on the other hand don't have any substantial meaning in this case - these classes are layout/design/markup oriented. 另一方面,
bottom-btn
和ga
类在这种情况下没有任何实质意义 - 这些类是面向布局/设计/标记的。
Try to use search by relative XPath
instead of by absolute XPath
or by class_name
: 尝试使用相对
XPath
而不是绝对XPath
或class_name
:
browser.find_element_by_xpath("//button[@class='show-more-cta bottom-btn ga']")
or 要么
browser.find_element_by_xpath("//button[.='Show More']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.