Can't access some content on page using selenium and python

python selenium

74 просмотра

2 ответа

246 Репутация автора

I am using selenium in a python script to login into a website where I can get an authorization key to access their API. I am able to login and navigate to the page where the authorization key is provided, I am using chrome driver for testing so I can see what's going on. When I get to the final page where the key is displayed, I can't find a way to access it. I can't see it in the page source, and when I try to access via the page element outer html, it doesn't print the value shown on the page. Here is a screenshot of what I see in the browser (I'm interested in accessing the content shown in response body):

enter image description here

this is the code snippet I am using to try to access the content:

auth_key = WebDriverWait(sel_browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="responseBodyContent"]')))
print auth_key.get_attribute("outerHTML")

and this is what the print statement returns:

<pre id="responseBodyContent"></pre>

I've also tried:

print auth_key.text

which returns nothing. Is there way I can extract this key from the page?

Автор: kflaw Источник Размещён: 08.11.2017 11:33

Ответы (2)

0 плюса

1 Репутация автора

Since attribute value is in json format for responseBodyContent try this

authkey_text = json.loads(auth_key.get_attribute) print str(authkey_text)

Автор: dev Размещён: 09.11.2017 03:48

1 плюс

3850 Репутация автора


It looks like you need a custom wait to wait for the element and then wait for text.
First, add a class, find element and then get innerHTML of the element. Finally, measure length of the string.
See my example below.

class element_text_not_empty(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
            element = driver.find_element(*self.locator)
                return element.get_attribute('innerHTML')
                return False
        except Exception as ex:
            print("Error while waiting: " + str(ex))
            return False

driver = webdriver.Chrome(chrome_path)
    print("Start wait")
    result = WebDriverWait(driver, 20).until(element_text_not_empty((By.XPATH, '//*[@id="responseBodyContent"]')))
except Exception as ex:
    print("Error: " + str(ex))
Автор: Buaban Размещён: 09.11.2017 06:37
Вопросы из категории :