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/

4
  • I believe "result" should target the <iframe> or <frame> tag. The driver has to switch to it in order to retrieve it's contents. Commented May 21, 2020 at 22:07
  • Exactly. I might be doing it wrong, though. browser.switch_to.frame(browser.find_element_by_xpath(result))´´´ Commented May 21, 2020 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 Commented May 21, 2020 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 Commented May 21, 2020 at 22:16

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

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

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.