如何获取属性值Selenium+;python
我正在Python中使用Selenium。我想获取属性['href']的值,并检查它是否与“#”不同,如果是如何获取属性值Selenium+;python,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在Python中使用Selenium。我想获取属性['href']的值,并检查它是否与“#”不同,如果是 因此,如果没有跳出循环,请单击链接: if not driver.find_element_by_link_text('»'): break # Open next page if driver.find_element_by_xpath("//a[.='»' and not(@href='#')]"): link=WebD
因此,如果没有跳出循环,请单击链接:
if not driver.find_element_by_link_text('»'):
break
# Open next page
if driver.find_element_by_xpath("//a[.='»' and not(@href='#')]"):
link=WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "»")))
link.click()
else:
break
以下是该网页的源代码:
你可以试试这个
elems = driver.find_elements_by_xpath("//a[@href]")
e = []
for elem in elems:
val = elem.get_attribute("href")
if "#" in val:
print("found # do nothing")
else:
print("click this link ",val)
// click link
e.append(elem)
// call click function
e[0].click()
要单击文本为>的链接,不需要获取属性href的值,只需将代码块包围起来,然后单击
块中的元素,如下所示:
while True:
try :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li/a[@rel='nofollow' and contains(.,'»')]"))).click()
# do your tasks on this particular page
except :
print("No more pages left")
break
driver.quit()
你的代码怎么了?你有什么错误吗?使用相关HTML和错误堆栈跟踪更新问题我得到此错误:AttributeError:'ResultSet'对象没有属性'attrs'这是所有相关代码吗?何时以及如何创建soup
?我已成功获取第一页中存在的所有数据并将其保存在csv文件中,我想检查下一页是否存在,然后单击链接并删除,但问题是即使下一页不存在且href=“#”的值我进入页面,因此我的程序永远不会停止。@MohammedBenaou,下一步按钮的HTML在哪里?(我的意思是“»”)但我想访问下一页的链接我的意思是“»”我尝试了sEnabled()和isClickable(),但不适用于mei获取此错误:e[0]。单击()索引器:列表索引超出范围使用len(e)检查列表的长度