Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97778

How to interact with an element with Selenium when I don't have it's id?

$
0
0

I want to send a message to [this webpage][1] with Python.

It is to say to do the following but with Python:

introducir la descripción de la imagen aquí

That's why I tried the following script with Selenium:

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

api_location = 'http://iphoneapp.spareroom.co.uk'
api_search_endpoint = 'flatshares'
api_details_endpoint = 'flatshares'

location = 'http://www.spareroom.co.uk'
details_endpoint = 'flatshare/flatshare_detail.pl?flatshare_id='

text = "Hello ! I am interested in your offer"
email = "myemail"
password = "mypassword"



def contact_room(room_id):
    url = '{location}/{endpoint}/{id}?format=json'.format(location=api_location, endpoint=details_endpoint, id=room_id)
    print(url)
    driver_path = 'C:\Program Files\chromedriver'
    driver = webdriver.Chrome(executable_path = driver_path )  # Optional argument, if not specified will search path.
    # Go to your page url
    driver.get(url)
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li.emailadvertiser > .button.button--wide"))).click()

    # writting the text
    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.ID, "message"))
    )  # Wait until the `text` element appear (up to 5 seconds)
    driver.find_element_by_id("message").clear()
    driver.find_element_by_id('message').send_keys(text)

    # writing my email, but I don't have the id of the element
    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "li.formlabel > .button.login_email"))
    )  # Wait until the `email` element appear (up to 5 seconds)
    driver.findElement(By.cssSelector("li.formlabel > .button.login_email")).sendKeys(email);

contact_room(4121595)

Everything runs well until trying to reach the email field. Indeed, I can't get its id as it seems there is none. So I tried driver.findElement(By.cssSelector("li.formlabel > .button.login_email")) but it gives me back :

(C:\Users\antoi\Anaconda2) C:\Users\antoi\Documents\Programming\projects\roomfinder>python test_message.py
http://iphoneapp.spareroom.co.uk/flatshare/flatshare_detail.pl?flatshare_id=/4121595?format=json

DevTools listening on ws://127.0.0.1:56296/devtools/browser/a1cf1399-78ea-4411-aa0a-53bf5352c359
[16004:3276:1114/024915.358:ERROR:wmi_refresher.cc(129)] Unable to add the Win32_PerfRawData_PerfDisk_PhysicalDisk enum.
Traceback (most recent call last):
  File "test_message.py", line 42, in <module>
    contact_room(4121595)
  File "test_message.py", line 37, in contact_room
    EC.presence_of_element_located((By.CSS_SELECTOR, "li.formlabel > .button.login_email"))
  File "C:\Users\antoi\Anaconda2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Viewing all articles
Browse latest Browse all 97778

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>