如何使用Python硒Web驱动程序chorme从XHR中的网络调用获取响应json数据

时间:2019-02-07 09:03:13

标签: python selenium selenium-webdriver

我需要python selenium Webdriver中来自网络调用的json数据,甚至来自网络调用的一个特定的url响应json数据

这是我想要的样本数据的图像

enter image description here

我确实想从网络调用的json响应中获取“ Mobikwik优惠”

任何人都可以帮我这个忙,还建议其他方法来获取此数据。预先感谢

3 个答案:

答案 0 :(得分:0)

您需要检查标题以获取请求网址。将该API网址与请求库一起使用。像这样:

import requests, json

url = *url here*
response = requests.get(url)
json_value = response.json()
alt = json_value['alt']

答案 1 :(得分:0)

您可以找到<?xml version="1.0" encoding="UTF-8"?> <jboss-web version="10.0" xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_10_0.xsd"> <context-root>cert-auth</context-root> <security-domain>client_cert_domain</security-domain> </jboss-web> 元素并获取其文本,然后通过<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <security-constraint> <display-name>SSL Authentication Constraint</display-name> <web-resource-collection> <web-resource-name>all</web-resource-name> <url-pattern>/secured/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>default</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>default</role-name> </security-role> <login-config> <auth-method>CLIENT-CERT</auth-method> <realm-name>client_cert_domain</realm-name> </login-config> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>

加载它
<Style x:Key="CreateNewFolderButton" TargetType="{x:Type Button}" BasedOn="{StaticResource HoverLess}">
    <Setter Property="ToolTip" Value="Create Folder"/>
    <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
    <Setter Property="IsHitTestVisible" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Grid >
                    <Ellipse Width="{StaticResource WidthNavigationAndCreation}" Height="{StaticResource HeightNavigationAndCreation}" />
                    <Ellipse Width="{StaticResource WidthNavigationAndCreation}" Height="{StaticResource HeightNavigationAndCreation}" x:Name="image" >
                        <Ellipse.Fill>
                            <ImageBrush  ImageSource="{DynamicResource AddFolder}" />
                        </Ellipse.Fill>
                    </Ellipse>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Fill"  TargetName="image" Value="{DynamicResource IBAddFolderMouseOver}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="MakeNewFolderButton"  TargetType="{x:Type Button}" BasedOn="{StaticResource FolderFileButtons}">
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate  TargetType="{x:Type ButtonBase}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="0 0 0 0.5">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Image Width="{StaticResource WidthFolderAndFilesIcon}"
                               Height="{StaticResource HeightFolderAndFilesIcon}" Source="/Images/FolderAndFiles/BlackEmptyFolder48x48.png"/>                            
                        <TextBox Grid.Column="1" Style="{StaticResource TextBoxEnterFileCreation}"
                                 Tag="New Folder Name"
                                 Text="{Binding RelativeSource={RelativeSource AncestorType=Window},
                                        Path=DataContext.NewFolderName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <Button Grid.Column="2" Style="{StaticResource CreateNewFolderButton}"
                                Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.MakeFolder}"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource ButtonBackgroundMouseOver}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有关更多信息,请访问

http://seleniummaster.com/sitecontent/index.php/selenium-web-driver-menu/selenium-test-automation-with-python/235-how-to-test-json-with-selenium-python

答案 2 :(得分:0)

您可以在此处将请求URL与requests一起使用。硒是不必要的。

import requests

url = 'https://s0.dominosindia.in/s0-olo-api-bucket/v1/widgetcontent/brandOffers'
find = 'Mobikwik Offer'  

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}

response = requests.get(url, headers=headers).json()
data = response['data']

for ele in data:
    if ele['alt'] == find:
        id_key = ele['id']
        imageUrl = ele['imageUrl']
        alt = ele['alt']

        print (ele)