【问题标题】:How to call Rest API and use the data in that API?如何调用 Rest API 并使用该 API 中的数据?
【发布时间】:2020-03-01 09:18:41
【问题描述】:

我有一个场景,我将打开我的网站,然后我必须调用 Rest API,在该 API 中我需要一些数据,比如用户名、密码和一些 URL。调用 API 的目的是我不知道要检查的凭据以及 URL,每次这些数据都会发生变化,因此我只会调用一个 API。 下面是我的 Selenium Python

from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# declare variable to store the URL to be visited
base_url="https://www.fitotouch.com/qitouch"

driver = webdriver.Chrome('E:/Chrome driver/chromedriver.exe')
driver.maximize_window()
#function of our 'driver' object.
driver.implicitly_wait(10) #10 is in seconds
driver.get(base_url)
driver.implicitly_wait(10)
driver.find_element_by_name('password').send_keys("*****")
driver.implicitly_wait(10)
driver.find_element_by_class_name('arrow-icon').click()

在上面的点击命令之后,我必须从 API 中给出的数据调用 API 和登录。

我的 API 是: http://110.93.230.117:1403/api/order/5e439b7052fcf2189ccb5207

如果我能在 Java 中获得解决方案,那也很好。

【问题讨论】:

    标签: java python selenium-webdriver


    【解决方案1】:

    使用requests模块获取数据作为字典,从那里你可以通过键获取值

    import requests
    
    data = requests.get('http://110.93.230.117:1403/api/order/5e439b7052fcf2189ccb5207').json()
    print(data)
    
    """
    {'date': '2020-02-12T06:30:08.106Z',
     '_id': '5e439b7052fcf2189ccb5207',
     'fitoName': 'Chinasor 01 - Bu Yang Huan Wu Wan',
     'fitoCode': 'Chinasor 01',
     'providerName': 'Soria - Chinasor',
     'providerCode': 'Chinasor 01',
     'valueItem': '01',
     'Email': 'helder@gmail.com',
     '__v': 0}
    """
    

    【讨论】:

    • 谢谢,但现在我将如何在我的 selenium 中使用 API 中的数据?意味着现在我已经给出了电子邮件的 xpath,并且电子邮件存在于 API 中。
    • @Maham 你的意思是你想要Email 的值吗? 'helder@gmail.com'?可以通过keydata['Email']获取值。
    • 我将如何定位电子邮件字段。我尝试了很多方法,但每次我收到无法找到元素的异常。附上屏幕截图。
    • @Maham 这是一个新问题,请用相关代码和html作为文本提出一个问题。但首先尝试等待它stackoverflow.com/questions/32398859/… 并检查它不在iframe 内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多