3

I am new to web scraping and I try to open a link with selenium:

On Google Chrome I use inspect on the button I want to open and I get the following information:

<a href="/c#candidates?id=a6b0e325a499&amp;candidateFilter=4af15d8991a8" data-tn-link="true" data-tn-element="view-unread-candidates"><span class="jobs-u-font--bold">(4 awaiting review)</span></a>

I try to get all the links with the same structure and open it so I can access its data

enter image description here enter image description here

(I have several button with the same structure but different href that I need to see)

Also under Properties I can see a for the same button.

However I want to be more precise than just using as I want only those particular links mentioned above:

elements = driver.find_elements_by_tag_name("a")

Can anyone advise ?

14
  • Can you show an additional link or two so we can see what remains fixed and what changes between candidates? For example, is it only unread you care about? It sounds like you really want to be able to match all qualifying a tags and then click - will you be passing a list of ids for example? Commented May 11, 2019 at 3:49
  • Thanks I added image so you can see the structure. Actually candidateFilter is not the issue here. Commented May 12, 2019 at 21:05
  • This solved now? Commented May 12, 2019 at 21:06
  • not really didn't make it work I think I get the elements with xpath: xpath = "/html/body/div[@id='wrapper']/div[@id='page_frame']/div[@id='page_content']/div[@class='page-wrapper']/div[@id='mc']/div[@id='plugin_container_MainContent']/div[@class='plugin-hadesinternal']/div/div/div[@class='jobs-JobsTab-main']/table[@class='jT cSST']/tbody/tr[@class='job-row'][3]/td[@class='candidates']/div/a[1]" selected_option = driver.find_element_by_xpath(xpath) But can't open in a new tab or open and return to my previous page or open with BeautifulSoup... Commented May 12, 2019 at 21:10
  • 1
    Thanks @QHarr. Working well. I just had to add an intermediary step in between. I will show full code so it's clearer selected_option = driver.find_element_by_xpath(xpath) url = selected_option.get_attribute("href") driver.execute_script("window.open('" + url + "');") Commented May 12, 2019 at 21:32

4 Answers 4

2

You can use //a[@data-tn-element = 'view-unread-candidates'], which will list all unread candidates.

If you want a specific candidate by candidate id then use the following xpath. And set the candidateId with the desired id.

candidateId = 'a6b0e325a499'
"//a[@data-tn-element = 'view-unread-candidates'][contains(@href,'id=" + candidateId + "')]"
Sign up to request clarification or add additional context in comments.

3 Comments

There seems to be something off in your quotes, as candidateId is a different color. I think the python interpreter might get confused by it, but I'm not sure I haven't tested it
its the variable holding the candidate id. @Reedinationer updated the answer for clear understanding.
Oh durrr it's just a concatenation. I usually use "stuff {} more stuff".format(variable) so it threw me off! Yeah, that would work I think. Not sure if OP has the candidateId beforehand though.
1

I would use

elem = driver.find_element_by_class_name("jobs-u-font--bold")

To get the <span>, since that seems like a unique class name (although I can't be sure from your post). Then you can reach the <a> level with

a_elem = elem.find_element_by_xpath("..")

Then you can a_elem.click() or whatever you are trying to do.

3 Comments

Hey, thanks for the reply. elem = driver.find_elements_by_class_name("jobs-u-font--bold") correctly returns a list of elements. I itterate in a loop and tried for e in elements: a_elem = e.parent a_elem.click() it returns: AttributeError: 'WebDriver' object has no attribute 'click'
@Solal Yeah, I must have gotten confused with beautifulsoup. After referencing this post I think you can leverage the same technique
Thanks I am using xpath and an extension Xpath Helper to grep the Xpath :) Working well
1

To access the anchor tag you can use css selector with attribute data-tn-element="view-unread-candidates" i believe it should be same for all anchor tag.

elements=driver.find_elements_by_css_selector('a[data-tn-element="view-unread-candidates"]')
for ele in elements:
    print(ele.get_attribute("href"))

Or if you want to use child element and then want to fetch the parent tag then try below code with xpath.

elements=driver.find_elements_by_xpath("//span[@class='jobs-u-font--bold']")
for ele in elements:
    print(ele.find_element_by_xpath("./parent::a").get_attribute('href'))

Comments

0

I would use:

List elements = driver.findElements(By.xpath("//a[@data-tn-element='view-unread-candidates']"));

    Iterator<WebElement> iter = elements.iterator();

    while (iter.hasNext()) {
        WebElement item = iter.next();
        String href = item.getAttribute("href");
        System.out.println("href is " + href);
    }
}

And if you want to click the link with the particular href, then you can put the if condition after getting the href in the above code . When that condition will meet, click on the element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.