使用selenium xpath获取描述

时间:2016-12-11 09:52:10

标签: selenium xpath automated-tests

我正在尝试获取求职页面Indeed.com的职位描述。这就是它的样子

enter image description here 提供技术领导力 QA 的  自动化到IT团队。与各种团队合作推广 QA 的  流程,实践和标准化......

我知道如何获得该描述?我尝试了以下方法: //跨度[含有(@class,'总结&#39)]

那不给我文字说明。我应该xpath还是有其他解决方案?提前感谢您的时间。

1 个答案:

答案 0 :(得分:1)

这个XPath是正确的。

element = driver.findElement(By.name("summary"));
element = driver.findElement(By.className("summary"));
element = driver.findElement(By.cssSelector('span[class="summary"]');

我是一个Python人,但我把它翻译成了Java。你可以这样做:

.getText()

请记住,如果你想要元素文本,每个元素都有方法find*driver.findElements(By.xpath())函数只检索元素。

仔细检查您没有使用复数.getText()。在这种情况下,您应该首先检索单个元素。然后访问description = driver.findElement(By.className("summary")).getText(); System.out.print(description); 方法。

description = driver.findElement(By.className("summary"));
description_text = description.getAttribute("innerHTML");
System.out.print(description_text);

或者你可以这样做:

element = driver.executeScript("return document.querySelector('span[class=\"summary\"]');");

如果您的问题是您的元素不可见或无法访问(陈旧)。然后你可以使用javascript。

#include <avr/io.h>
#include <avr/interrupt.h>
 #include <stdlib.h>
//#define F_CPU 16000000UL
#define F_CPU 8000000
#include <util/delay.h>
#define ADC_VREF_TYPE 0x40
//#include "adc_new.h"
#define D4 eS_PORTC4
#define D5 eS_PORTC5
#define D6 eS_PORTC6
#define D7 eS_PORTC7
#define RS eS_PORTC2
#define EN eS_PORTC3
#include "lcd.h" //Can be download from the bottom of this article
//  Constants and variables
//*****************************************************************************
// Input: Square wave on ICP PIN
// This program determines the pulse width of a square wave and if the pulse width is greater than 40 us
//than PD4 goes higher ,if its smaller than PD4 is low.

//---------------------------------------------------------//
void LCD_0(void)
{
    /*
' Lcd module connections
dim LCD_RS as sbit at PORTc2_bit
dim LCD_EN as sbit at PORTc3_bit
dim LCD_D4 as sbit at PORTc4_bit
dim LCD_D5 as sbit at PORTc5_bit
dim LCD_D6 as sbit at PORTc6_bit
dim LCD_D7 as sbit at PORTc7_bit

dim LCD_RS_Direction as sbit at DDc2_bit
dim LCD_EN_Direction as sbit at DDc3_bit
dim LCD_D4_Direction as sbit at DDc4_bit
dim LCD_D5_Direction as sbit at DDc5_bit
dim LCD_D6_Direction as sbit at DDc6_bit
dim LCD_D7_Direction as sbit at DDc7_bit

*/

DDRC = 0xFF;


Lcd4_Init();
Lcd4_Set_Cursor(1,1);
Lcd4_Write_String("Elasa.ir Test"); 
}   

//http://www.geeksforgeeks.org/convert-floating-point-number-string/
// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
    // Extract integer part
    int ipart = (int)n;

    // Extract floating part
    float fpart = n - (float)ipart;

    // convert integer part to string
    int i = itoa(ipart, res, 0);

    // check for display option after point
    if (afterpoint != 0)
    {
        res[i] = '.';  // add dot

        // Get the value of fraction part upto given no.
        // of points after dot. The third parameter is needed
        // to handle cases like 233.007
        fpart = fpart * pow(10, afterpoint);

        itoa((int)fpart, res + i + 1, afterpoint);
    }
}


int main(void) {
DDRD = (0<<PD4);     // put PortB bit 5 as input
//PORTD = 0<<PD4;       // Enable PE4 pull-up resistor
DDRC = 0xFF;
//enable overflow and input capture interrupts

TIMSK=0x24;

/*Noise canceller, without prescaler, rising edge*/

TCCR1B=0xC1;



// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: Int., cap. on AREF
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;_delay_ms(30);


Lcd4_Init();

Lcd4_Set_Cursor(1,1);   
Lcd4_Write_String("Pulse width measuring:");
//Lcd4_Set_Cursor(2,1); 

//itoa(pulse_width2, str3, 10);

_delay_ms(3);



    while(1)
    {
        //http://www.edaboard.com/thread63677.html
        char buffer22[24];
        float x = 1.5;

        sprintf(buffer22, "Flo %f", x);
        Lcd4_Clear();
        Lcd4_Set_Cursor(1,1);   
        Lcd4_Write_String("Temp_eda:");
        Lcd4_Set_Cursor(2,1);
        Lcd4_Write_String(buffer22);
        _delay_ms(300);


        char res[20];
        float n = 233.007;
        ftoa(n, res, 4);


        //ftoa3(myFloatStr, myFloat, 10);
        //Lcd4_Write_String(myFloatStr);
        Lcd4_Clear();
        Lcd4_Set_Cursor(1,1);   
        Lcd4_Write_String("Temp_PT100:");
        Lcd4_Set_Cursor(2,1);
        Lcd4_Write_String(res);
        _delay_ms(300);

    }
}

更多参考: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html