How to get attribute value Selenium + Python

前端 未结 2 1124
挽巷
挽巷 2021-01-29 06:00

I\'m working with Selenium in Python. I would like to get the value of an attribute [\'href\'] and check that it is different from \'#\', if yes
so click on the link if not

相关标签:
2条回答
  • 2021-01-29 06:53

    To click on the link with text as » you don't need to get the value of an attribute href, rather simply surround the block of code to click() the element in a try-catch block as follows:

    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()
    
    0 讨论(0)
  • 2021-01-29 06:58

    You could try this,

    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()
    
    0 讨论(0)
提交回复
热议问题