简体   繁体   English

Selenium Webdriver - Python - leboncoin (classified) - pb 选择下拉列表和框

[英]Selenium Webdriver - Python - leboncoin (classified) - pb to select a dropdown list & box

I continue my path on the learning curve of Python/Selenium.我继续我的 Python/Selenium 学习曲线。

Selenium Webdriver - Python - leboncoin - pb to select a button with an accent Selenium Webdriver - Python - leboncoin - pb 以选择带有重音的按钮

After successfully connected to the site, and selecting the button, I would like to populate the fields to submit a classified ad.成功连接到网站并选择按钮后,我想填充字段以提交分类广告。

At this stage, there are 2 fields I am trying to select, a dropdown list and a box.在这个阶段,我尝试选择 2 个字段,一个下拉列表和一个框。 the Html is the below Html 如下

Dropdown list下拉列表

<div class="selectWrapper single">
<select id="category" class="select" name="category">
<option value="0">«Choisissez une catégorie»</option>
<option id="cat71" value="71" style="background-color:#E6E6E6" disabled="">-- EMPLOI --</option>
<option id="cat33" value="33">Offres d'emploi</option>
<option id="cat1" value="1" style="background-color:#E6E6E6" disabled="">-- VEHICULES --</option>
<option id="cat2" value="2">Voitures</option>
<option id="cat3" value="3">Motos</option>
<option id="cat4" value="4">Caravaning</option>
<option id="cat5" value="5">Utilitaires</option>
<option id="cat6" value="6">Equipement Auto</option>
<option id="cat44" value="44">Equipement Moto</option>
<option id="cat50" value="50">Equipement Caravaning</option>
<option id="cat7" value="7">Nautisme</option>
<option id="cat51" value="51">Equipement Nautisme</option>
<option id="cat8" value="8" style="background-color:#E6E6E6" disabled="">-- IMMOBILIER --</option>
<option id="cat9" value="9">Ventes immobilières</option>
<option id="cat10" value="10">Locations</option>
<option id="cat11" value="11">Colocations</option>
<option id="cat13" value="13">Bureaux & Commerces</option>
<option id="cat66" value="66" style="background-color:#E6E6E6" disabled="">-- VACANCES --</option>
<option id="cat12" value="12">Locations & Gîtes</option>
<option id="cat67" value="67">Chambres d'hôtes</option>
<option id="cat68" value="68">Campings</option>
<option id="cat69" value="69">Hôtels</option>
<option id="cat70" value="70">Hébergements insolites</option>
<option id="cat14" value="14" style="background-color:#E6E6E6" disabled="">-- MULTIMEDIA --</option>
<option id="cat15" value="15">Informatique</option>
<option id="cat43" value="43">Consoles & Jeux vidéo</option>
<option id="cat16" value="16">Image & Son</option>
<option id="cat17" value="17">Téléphonie</option>
<option id="cat18" value="18" style="background-color:#E6E6E6" disabled="">-- MAISON --</option>
<option id="cat19" value="19">Ameublement</option>
<option id="cat20" value="20">Electroménager</option>
<option id="cat45" value="45">Arts de la table</option>
<option id="cat39" value="39">Décoration</option>
<option id="cat46" value="46">Linge de maison</option>
<option id="cat21" value="21">Bricolage</option>
<option id="cat52" value="52">Jardinage</option>
<option id="cat22" value="22">Vêtements</option>
<option id="cat53" value="53">Chaussures</option>
<option id="cat47" value="47">Accessoires & Bagagerie</option>
<option id="cat42" value="42">Montres & Bijoux</option>
<option id="cat23" value="23">Equipement bébé</option>
<option id="cat54" value="54">Vêtements bébé</option>
<option id="cat24" value="24" style="background-color:#E6E6E6" disabled="">-- LOISIRS --</option>
<option id="cat25" value="25">DVD / Films</option>
<option id="cat26" value="26">CD / Musique</option>
<option id="cat27" value="27">Livres</option>
<option id="cat28" value="28">Animaux</option>
<option id="cat55" value="55">Vélos</option>
<option id="cat29" value="29">Sports & Hobbies</option>
<option id="cat30" value="30">Instruments de musique</option>
<option id="cat40" value="40">Collection</option>
<option id="cat41" value="41">Jeux & Jouets</option>
<option id="cat48" value="48">Vins & Gastronomie</option>
<option id="cat56" value="56" style="background-color:#E6E6E6" disabled="">-- MATERIEL PROFESSIONNEL --</option>
<option id="cat57" value="57">Matériel Agricole</option>
<option id="cat58" value="58">Transport - Manutention</option>
<option id="cat59" value="59">BTP - Chantier Gros-oeuvre</option>
<option id="cat60" value="60">Outillage - Matériaux 2nd-oeuvre</option>
<option id="cat32" value="32">Équipements Industriels</option>
<option id="cat61" value="61">Restauration - Hôtellerie</option>
<option id="cat62" value="62">Fournitures de Bureau</option>
<option id="cat63" value="63">Commerces & Marchés</option>
<option id="cat64" value="64">Matériel Médical</option>
<option id="cat31" value="31" style="background-color:#E6E6E6" disabled="">-- SERVICES --</option>
<option id="cat34" value="34">Prestations de services</option>
<option id="cat35" value="35">Billetterie</option>
<option id="cat49" value="49">Evénements</option>
<option id="cat36" value="36">Cours particuliers</option>
<option id="cat65" value="65">Covoiturage</option>
<option id="cat37" value="37" style="background-color:#E6E6E6" disabled="">-- -- --</option>
<option id="cat38" value="38">Autres</option>
</select>
</div>
<span class="label-error full " data-for="category"></span>
</div>

Box盒子

<div class="inputWrapper">
<input id="subject" class="nude" name="subject" maxlength="50" value="" type="text">
<span class="unit"></span>
</div>

As there are no accent, It thought I could directly use their tag and the code is the below由于没有口音,我以为我可以直接使用他们的标签,代码如下

Python code:蟒蛇代码:

Select(driver.find_element_by_id("category")).select_by_visible_text("Locations")

driver.find_element_by_id("subject").send_keys("text to insert")

For both, I have an error.对于两者,我都有一个错误。

Error message错误信息

Traceback (most recent call last):
  File "connectionwebdriver2", line 31, in test_connectionwebdriver2
    driver.find_element_by_xpath("//*[@id='cat10']").click()
  File "/Users/olivierhoen/scrap/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/olivierhoen/scrap/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
    'value': value})['value']
  File "/Users/olivierhoen/scrap/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Users/olivierhoen/scrap/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

Though I have read different posts and selenium documentation, I am a bit stuck虽然我阅读了不同的帖子和 selenium 文档,但我有点卡住了

Thanks in advance for your support预先感谢您的支持

Best,最好的事物,

Full Python code (I have set up specific credential)完整的 Python 代码(我已经设置了特定的凭据)

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Connectionwebdriver2(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Safari()
        self.driver.maximize_window()
        self.base_urldr = "https://compteperso.leboncoin.fr/account/index.html"
        self.verificationErrors = []
        self.accept_next_alert = True
        self.driver.implicitly_wait(10)

    def test_connectionwebdriver2(self):
        driver = self.driver
        wait = WebDriverWait(driver,10)
        driver.get(self.base_urldr)
        driver.find_element_by_name("st_username").clear()
        driver.find_element_by_name("st_username").send_keys("thecoingood@gmail.com")
        driver.find_element_by_name("st_passwd").clear()
        driver.find_element_by_name("st_passwd").send_keys("thecoingood1")
        driver.find_element_by_id("connect_button").click()
        #driver.get("https://www2.leboncoin.fr/ai?ca=15_s")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='deposer']"))).click()
        driver.find_element_by_xpath("//*[@id='cat10']").click()
        driver.find_element_by_xpath("//*[@id='subject']").send_keys("test")
        time.sleep(10)

This should work for you :)这应该对你有用:)

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.leboncoin.fr/ai/form/0")
driver.find_element_by_xpath("//*[@id='cat10']").click()
driver.find_element_by_xpath("//*[@id='subject']").send_keys("test")

and to find the xpath of an element do this: Gif并找到元素的 xpath 这样做: Gif

Edit: Just did some modifications to your code to test it, and it works perfectly in firefox.编辑:只是对你的代码做了一些修改来测试它,它在 Firefox 中完美运行。

driver = webdriver.Firefox()
driver.maximize_window()
base_urldr = "https://compteperso.leboncoin.fr/account/index.html"
verificationErrors = []
accept_next_alert = True
driver.implicitly_wait(10)

wait = WebDriverWait(driver,10)
driver.get(base_urldr)
driver.find_element_by_name("st_username").clear()
driver.find_element_by_name("st_username").send_keys("thecoingood@gmail.com")
driver.find_element_by_name("st_passwd").clear()
driver.find_element_by_name("st_passwd").send_keys("thecoingood1")
driver.find_element_by_id("connect_button").click()
#driver.get("https://www2.leboncoin.fr/ai?ca=15_s")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='deposer']"))).click()
driver.find_element_by_xpath("//*[@id='cat10']").click()
driver.find_element_by_xpath("//*[@id='subject']").send_keys("test")
time.sleep(10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Webdriver - Python - leboncoin - pb 选择带重音的按钮 - Selenium Webdriver - Python - leboncoin - pb to select a button with an accent Python Selenium webdriver 获取 XPATH 和 select 下拉列表 - Python Selenium webdriver get XPATH and select dropdown Python Selenium Webdriver选择下拉值 - Python Selenium Webdriver Select Dropdown Value 如何使用Selenium WebDriver单击或选择Python中输入下拉框的第一个元素(值) - how to click or select on very first element(value) of input dropdown box in python using selenium webdriver 使用Selenium python选择一个下拉框 - select a dropdown box using Selenium python 铅到 select 下拉列表中的文本 - Pb to select a text from a dropdown list 无法使用Selenium WebDriver从下拉列表中选择元素 - Unable to select an element from a dropdown list with selenium webdriver Python Selenium WebDriver下拉菜单如何选择项目 - Python selenium webdriver dropdown menu how to select items 如何使用 selenium WebDriver python 遍历每个下拉列表 - How to traverse each dropdown list using selenium WebDriver python 使用Python和Selenium Webdriver,根据文本值从运行时生成的复选框列表中选择一个复选框 - Select a check box from a list of run time generated check boxes on the basis of a text value, using Python and Selenium Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM