简体   繁体   中英

Select proper element on webpage using Python - Selenium chromedriver

I am trying to write script which needs to open two boxes on webpage and get proper email address. I believe i am not inheriting elements on webpage properly. Please view my code below:

   try:
            WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > section > section > section > div.mapAndAttrs > p:nth-child(2)")))
            xxx = driver.find_element_by_css_selector("#titletextonly").text
            #Click on Reply button
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.reply-button.js-only"))).click()
            WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > section > section > header > div.reply-button-row > div > div.reply-info.js-only')))
            #target email address
            to = driver.find_element_by_css_selector("body > section > section > header > div.reply-button-row > div > div.reply-info.js-only > aside.reply-flap.js-captcha > ul > li.reply-email > p > a").text
        except:

Here is sample page from which I need to extract email: https://newyork.craigslist.org/fct/cto/d/ridgefield-2003-bmw-x5-30-xdrive/7212695001.html

Email is located in "reply" button and then another "show email" button.

Any guidance is greatly appreciated.

you can use:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > section > section > section > div.mapAndAttrs > p:nth-child(2)")))
xxx = driver.find_element_by_css_selector("#titletextonly").text
#Click on Reply button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.reply-button.js-only"))).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body > section > section > header > div.reply-button-row > div > div.reply-info.js-only')))
#target email address
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > section > section > header > div.reply-button-row > div > div.reply-info.js-only > aside > ul > button'))).click()
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > section > section > header > div.reply-button-row > div > div.reply-info.js-only > aside')))
texto = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body > section > section > header > div.reply-button-row > div > div.reply-info.js-only > aside > ul > li.reply-email > p > a'))).text

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