简体   繁体   中英

How to get the href value when it is in JavaScript using Python + Selenium

在此处输入图片说明

In the above HTML, I would like to get the Value "INC3000..." . But if I use get_attribute() the value returned is None as it is rendered in JavaScript during runtime.

How can I get the value of "href=javascript:" (INC30000004609 (New)) ?

You need to use explicit wait for the result ready. JS rendered element cannot be obtained without waiting. Neither simple requests, nor selenium without waiting.

wait = WebDriverWait(driver, 10)
element = wait.until(element_has_text(By.XPATH, '//a[contains(@class, "btn")]'))    
class element_has_text(object):

    def __init__(self, locator, css_class):
        self.locator = locator

    def __call__(self, driver):
        element = driver.find_element(*self.locator)   # Finding the referenced element
        if element.text:
            return element
        else:
            return False   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM