简体   繁体   English

无法使用Selenium和python访问页面上的某些内容

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

I am using selenium in a python script to login into a website where I can get an authorization key to access their API. 我在python脚本中使用硒登录网站,在那里我可以获取授权密钥来访问其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. 我能够登录并导航到提供授权密钥的页面,我正在使用chrome驱动程序进行测试,以便了解发生了什么。 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. 我在页面源代码中看不到它,并且当我尝试通过外部HTML页面元素进行访问时,它不会打印页面上显示的值。 Here is a screenshot of what I see in the browser (I'm interested in accessing the content shown in response body): 这是我在浏览器中看到的屏幕截图(我有兴趣访问响应正文中显示的内容):

在此处输入图片说明

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: 这是print语句返回的内容:

<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? 有什么办法可以从页面中提取此密钥?

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. 首先,添加一个类,查找元素,然后获取该元素的innerHTML 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):
        try:
            element = driver.find_element(*self.locator)
            if(len(element.get_attribute('innerHTML').strip())>0):
                return element.get_attribute('innerHTML')
            else:
                return False
        except Exception as ex:
            print("Error while waiting: " + str(ex))
            return False

driver = webdriver.Chrome(chrome_path)
...
...
try:
    print("Start wait")
    result = WebDriverWait(driver, 20).until(element_text_not_empty((By.XPATH, '//*[@id="responseBodyContent"]')))
    print(result)
except Exception as ex:
    print("Error: " + str(ex))

Since attribute value is in json format for responseBodyContent try this 由于属性值是responseBodyContent的json格式,请尝试以下操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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