我正在尝试从IBM服务页面获取所有服务标题,并且出现以下错误:
我想知道 下一页是否存在 。因此,我可能会打破循环。或让我的循环重复那么多次。
这是我的代码:
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
def writefile(links):
with open('url_list.txt', 'w') as file:
file.writelines("%s\n" % link for link in links)
start_url = "https://www.ibm.com/us-en/products/categories?size=30&selectedTopicRoot=technologyTopics&types[0]=service"
links = []
chrome_path = r"C:\Users\IBM_ADMIN\Anaconda3\selenium\webdriver\Chrome\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get(start_url)
time.sleep(15)
while True:
time.sleep(5)
results = driver.find_elements_by_class_name("offering--name")
for i in range(len(results)):
links.append(results[i].text)
writefile(links)
try:
next = driver.find_element_by_xpath('//*[@id="IBMAccessibleItemComponents-next"]')
if (next.is_enabled()):
next.click()
else:
break
except NoSuchElementException:
break
driver.close()
答案 0 :(得分:2)
只有启用了XPath,您才可以使用更具体的XPath选择“下一步”按钮:
try:
driver.find_element_by_xpath('//*[@id="IBMAccessibleItemComponents-next" and not(@aria-disabled)]').click()
except NoSuchElementException:
break
请注意,最后一页aria-disabled="true"
已添加到“下一步”按钮的属性,因此//*[@id="IBMAccessibleItemComponents-next" and not(@aria-disabled)]
XPath不会将其匹配