没有ID时如何与Selenium元素交互?

时间:2019-11-14 01:56:26

标签: python python-3.x selenium selenium-webdriver

我想用Python向[此网页] [1]发送一条消息。

可以使用Python进行以下操作:

introducir la descripción de la imagen aquí

这就是为什么我用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)

一切正常,直到尝试访问电子邮件字段。确实,我无法获得它的ID,因为似乎没有。所以我尝试了driver.findElement(By.cssSelector("li.formlabel > .button.login_email")),但它给了我:

(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:

1 个答案:

答案 0 :(得分:0)

也许您在电子邮件字段中找到了错误的元素。

我在页面上找不到'.button.login_email'。

请记住,您只能将键发送到 input 元素。

尝试在CSS选择器下面使用:

input[name="login_email"]

您可以这样编写CSS选择器:

  

tag [attribute =“ xxx”]

enter image description here

参考:https://www.w3.org/TR/selectors-3/#selectors

相关问题