-1

Given below is the snippet of HTML:

<div class="a-row a-spacing-none">
    <span class="a-size-small a-color-secondary">by 
    </span>
    <span class="a-size-small a-color-secondary">
        <a class="a-link-normal a-text-normal" href="/Lowell-Fryman/e/B01M3MNJTE/ref=sr_ntt_srch_lnk_1?qid=1550228622&amp;sr=1-1">
        Lowell Fryman
        </a> 
        and 
    </span>
    <span class="a-size-small a-color-secondary">
        <a class="a-link-normal a-text-normal" href="/Gregory-Lampshire/e/B01N7ZWT5Y/ref=sr_ntt_srch_lnk_1?qid=1550228622&amp;sr=1-1">
        Gregory Lampshire
        </a>
    </span>
</div>

I'm trying to obtain the name of all authors. This is whatever test follows the word by. I came up with the following XPath but it doesn't seem to fetch all the authors.

My XPath expression:

//div//span[text()=\"by \"]//following::span[1]//a

Can someone please tell me how to obtain the name of all the authors while somehow managing to skip any element whose text is "and"?

I'm using Selenium's find_element_by_xpath if it helps.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Dhiwakar Ravikumar
  • 1,983
  • 2
  • 21
  • 36

4 Answers4

1

Here xpath you can use to get authors:

//div[./span[normalize-space(.='by')]]//a

or

//div[./span[contains(.,'by')]]//a


Your xpath should be like this:

//span[normalize-space(.='by')]/following-sibling::span//a
Sers
  • 12,047
  • 2
  • 12
  • 31
1

Try the below code.This should work.It will fetch all author.

elements=driver.find_elements_by_xpath("//a[@class='a-link-normal a-text-normal']")

for element in elements:
    print(element.text)

Please let me know if this work.

KunduK
  • 32,888
  • 5
  • 17
  • 41
1

you can first get text of all span elements in a list and then slice it from "by" text value

elements = [_.text() for _ in driver.find_elements_by_css_selector('div.span')]
print elements[elements.index('by'):] 
0

To print the name of all the authors which are followed by the word by you can use either of the following solutions:

  • Using innerHTML:

    print([author.get_attribute("innerHTML") for author in driver.find_elements_by_xpath("//span[contains(., 'by')]//following::span/a[@class='a-link-normal a-text-normal'][@href]")])
    
  • Using text:

    print([author.text for author in driver.find_elements_by_xpath("//span[contains(., 'by')]//following::span/a[@class='a-link-normal a-text-normal'][@href]")])
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352