简体   繁体   中英

Selenium Webdriver get element (Python)

I am making a bot that scrapes dictionary definitions. It first goes to this site: LINE Dictonary

It then looks up the word "你好", and there following results show: 在此处输入图片说明

My goal is to have the bot click on the first result. However, how would I do that using Selenium Webdriver?

在此处输入图片说明

Here is a snippet of what the HTML looks like for the result. However, if you would like to see all of it, please go to LINE Dictonary's website and type in "你好". Thank you!

You can use this XPATH :

//div[@id='autocplt_wrap']//descendant::span[@class='word']//a[text()='你好']

after searching for this : "你好" you can use this code :

asian_text= WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//div[@id='autocplt_wrap']//descendant::span[@class='word']//a[text()='你好']")))  
asian_text.click();  

Note that you can not use the linkText as there are two web Element present with same text between anchor tags.

you need to use Waits to do this, because you element was hidden, you can try this, it will work

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("http://ce.linedict.com/#/cnen/search?query=%E4%BD%A0%E5%A5%BD")
try:
    element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="autocplt_wrap"]/ul[1]/li/span/a[1]'))
    )
    element.click()
finally:
    driver.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM