Home > OS >  How to find element in <a href> (not have id, class)
How to find element in <a href> (not have id, class)

Time:05-01

I tried to get questioner list in enter image description here

CodePudding user response:

I would recommend looking at BeautifulSoup: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#quick-start

CodePudding user response:

Please have a look at the following. Check if this satisfies your requirements.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.options import Options
import time


browser = webdriver.Chrome(executable_path="chromedriver.exe")
browser.get("http://9nice.site")

link1 = browser.find_element_by_xpath("/html/body/header/section[2]/div/div[2]/div/div/div/nav[1]/ul/li/a")
link1.click()
link2 = browser.find_elements_by_xpath("//div[@class='dwqa-question-item']")
time.sleep(2)

for iteration, item in enumerate(link2):
        iteration  = 1
        question = browser.find_element_by_xpath(f"//div[@class='dwqa-question-item'][{iteration}]")
        who_asked_question = browser.find_element_by_xpath(f"//div[@class='dwqa-question-item'][{iteration}]/div[@class='dwqa-question-meta']/span/a")
        print(who_asked_question.text)
time.sleep(5)
browser.close()

Output:

Linh Nhi
Qúy
kuj9xst3
kuj9xst3
kuj9xst3
  • Related