Skip to content
Advertisement

Selenium is unable to get element.text while using python and web driver

JavaScript

I am learning Selenium with Python. Above is my code. I want to retrieve this circled element in Wikipedia, as we can see it is an anchor tag<a> that is within a <div> that has an id of “articlecount”. I’ve tried the By.CSS_SELECTOR which didn’t work, so I use XPATH instead as shown in the code. But it still doesn’t work.

Then I find out that if I print the articles variable alone, the code won’t raise an error. However, as soon as I print articles.text or articles.get_attribute("textContent"), the error still occurs.

Would be great if anybody can give me a helping hand. Thanks in advance.

enter image description here

enter image description here

Advertisement

Answer

You have a small bug. In this line, you are clicking on the element after finding it, which causes the page to change and you cant find the article count.

JavaScript

Just remove the click() and it will work

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement