1

I'm trying to automate accessing a Wikipedia page (without api) and parse the text, I can input the research value but struggle to select the first result from the input dropdown results (please open wikipedia if you don't know what I'm talking about). I tried extracting the XPATH from the webpage but still failed to make it work.
What I've tried

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Main_Page")
elem = driver.find_element(By.NAME, "search")
elem.send_keys('Python')
elem = driver.find_element(By.XPATH, '/html/body/div[5]/div/a[2]/div')
elem.send_keys(Keys.RETURN)

Thanks

1 Answer 1

1

I think a simpler way would be to add the search term into the link itself by combining strings (like f-strings). For example, if you want to search for "Python", you could do:

search_term = "Python"
url = f"https://en.wikipedia.org/wiki/{search_term}"
driver.get(url)

Hope this solves the issue

Sign up to request clarification or add additional context in comments.

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.