0

I am trying to scrape filling a form and submitting it. Filling and submitting works fine but I am interested in the pop message returned. So far I am not able to receive the message output. This is my code using Selenium Python.

import time 
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys


postnummer = [42435, 42448]
street_name =["Bredfjällsgatan", "Fjällviolen"]
street_number = [12, 7]


inputPost = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[1]'
inputStreet = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[2]'
inputNumber = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/input[3]'

submitButton = '//*[@id="body"]/div[3]/div/div/div[5]/div/div[2]/div/a/form/span[4]'

result = '//*[@id="body"]/div[1]/div/div/div/div[1]/div/p/text()[1]'


def sleep():
    time.sleep(3)

browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get("https://www.framtidensbredband.se/")
browser.find_element_by_xpath(inputPost).send_keys(postnummer[0])
browser.find_element_by_xpath(inputStreet).send_keys(street_name[0])
browser.find_element_by_xpath(inputNumber).send_keys(street_number[0])
sleep()
browser.find_element_by_xpath(submitButton).click()

#print(browser.find_element_by_xpath(result))

sleep()
browser.switch_to.frame(browser.find_element_by_xpath(result))
sleep()
browser.back()
sleep()

browser.quit()

The website being scraped is https://www.framtidensbredband.se/

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
MKJ
  • 369
  • 4
  • 17
  • I believe "result" should target the – pcalkins May 21 '20 at 22:07
  • Exactly. I might be doing it wrong, though. browser.switch_to.frame(browser.find_element_by_xpath(result))´´´ – MKJ May 21 '20 at 22:12
  • you change frame but you do not take action why are you trying to change frame, do you want to print or do you want to click – AomineDaici May 21 '20 at 22:14
  • I was able to get the text by simply `print(browser.find_element_by_css_selector(".alertbox .message").text)`. Try that just after you submit – Prab G May 21 '20 at 22:16

2 Answers2

1

if you want to text

1- you need to select the element as text

elem = driver.find_element_by_xpath("//*[@class='message']").text
print(elem)

2- you need to call the element as text

elem = driver.find_element_by_xpath("//*[@class='message']")
print(elem.text)
AomineDaici
  • 381
  • 2
  • 7
1

To print the pop up message output you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

  • Using XPATH:

    driver.get("https://www.framtidensbredband.se/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='postCode']"))).send_keys("42435")
    driver.find_element_by_xpath("//input[@name='street']").send_keys("Bredfjällsgatan")
    driver.find_element_by_xpath("//input[@name='streetNumber']").send_keys("12")
    driver.find_element_by_xpath("//span[@class='banner-button submit-button']").click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='alertbox']//p"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Console Output:

    Från och med 2020-12-31 kan du bli kund i det öppna bredbandsnätet. För att hitta och förbeställa tjänster gå in under Tjänster.
    
  • Browser Snapshot:

framtidensbredband

DebanjanB
  • 118,661
  • 30
  • 168
  • 217