无法使用xpath单击“下一步”按钮,.select或使用Python的Selenium

2024-10-03 02:48:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试单击此页底部的“下一步”按钮:https://www.domcop.com/domains/great-expired-domains/

我尝试过使用.select、xpath和Selenium使用css选择器,但没有任何效果。在

我认为这个按钮是在生成一个JQuery代码来列出同一页面上的域,这将解释python为什么不能用xpath和.select单击它。在

但是,我不知道如何用Selenium点击它。。。在

这是我的剧本:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.domcop.com/domains/great-expired-domains/")
assert "Python" in driver.title
link = driver.find_element_by_class_name('next').get_attribute('a')

它返回一个'NoneType'对象。。。在

任何帮助都将不胜感激!在

谢谢你


Tags: httpscomgetwwwdriverseleniumselect按钮
3条回答

enter image description here查找元素(:xpath,'//*[@id=“domcop-table_wrapper”]/div[position()>;1]/div[position()>;1]/div/ul/li[6]/a/i[@class=“icon double angle right”]')

此Xpath始终有效。之后可以向下滚动并单击

我只是没有向下滚动。仅此而已。在

检查此Xpath

(:xpath, '//*[@id="domcop-table_wrapper"]/div[not(contains(text(),
"Display")) and not(contains(text(),"records"))]/descendant::li[@class="next"]/a/i[@class="icon-double-angle-right"]')
  1. 你必须找到两个按钮。在
  2. 向下滚动到底部。在
  3. 单击第二个按钮。在

参见以下代码:

driver.get('https://www.domcop.com/domains/great-expired-domains/')    
aElements = driver.find_elements_by_css_selector('.next > a')
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);') 
aElements[1].click()

与常见的XML/HTML解析器不同,selenium模拟浏览器行为,因此它应该不会对JQuery生成的元素产生问题,正如您所担心的那样。选择器的问题是,a是类为^{的元素的not attribute,而是next子级。因此,您可以在此处为子项>使用CSS选择器:

link = driver.find_element_by_css_selector('.next > a')
link.click()

相关问题 更多 >