从Selenium + Python获取文本

时间:2018-09-17 21:33:56

标签: python html selenium element whatsapp

enter image description here

无法获得“您无法加入此群组,因为该群组已满。”作为文本。请帮我。 也请使用Beautiful Soup告诉我代码。

2 个答案:

答案 0 :(得分:0)

假设类名不变,则可以使用:

browser.find_elements_by_xpath("//div[contains(@class,'_31LzD')]/div")

如果类确实发生了变化,则可以始终从.copyable-area之类的“固定”元素开始工作:

browser.find_elements_by_xpath("//div[contains(@class,'copyable-area')]/div/div/div/div/div")

如果您知道文本本身不会更改,则可以根据文本内容进行查询:

driver.find_elements_by_xpath("//div[contains(text(), 'You can't join this group because it is full')]")

答案 1 :(得分:0)

使用XPath

static void Main(string[] args)
{
    String[] labels = { "Green", "Red", "Yellow", "Dark Red", "Blue" };
    Color[] colours = { Color.Green, Color.Red, Color.Yellow, Color.DarkRed, Color.Blue };
    Random rng = new Random();

    Chart chart = new Chart();
    chart.Size = new Size(320, 320);

    ChartArea area = new ChartArea();
    Series series = new Series();

    chart.ChartAreas.Add(area);

    series.ChartArea = area.Name;
    series.ChartType = SeriesChartType.Doughnut;
    series.IsValueShownAsLabel = true;

    int total = 0;
    for (int i = 0; i != labels.Length; i++)
    {
        int value = rng.Next(0, 50);
        DataPoint p = new DataPoint(0, value);
        total += value;
        p.Color = colours[i];
        p.Label = String.Empty;
        p.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
        series.Points.Add(p);
    }

    series.Tag = total;
    chart.Series.Add(series);
    chart.Refresh();

    using (Graphics g = chart.CreateGraphics())
    {
        String str = series.Tag.ToString();
        Font font = new Font("Microsoft Sans Serif", 32, FontStyle.Bold);
        SizeF strSize = g.MeasureString(str, font);

        int strX = 100; int strY = 100;

        g.DrawString(str, font, new SolidBrush(Color.Black), strX, strY);
        g.DrawRectangle(new Pen(Color.Black), new Rectangle(strX, strY, (int)strSize.Width, (int)strSize.Height));

        g.Flush();
    }

    String chartFilename = "chart.bmp";
    String chartPath = Path.Combine("C:\\Temp", chartFilename);

    using (Bitmap bmp = new Bitmap(chart.Width, chart.Height))
    {
        chart.DrawToBitmap(bmp, chart.Bounds);
        bmp.Save(chartPath);
    }

    System.Diagnostics.Process.Start("mspaint.exe", chartPath);
}