mardi 27 décembre 2016

Check a checkbox in an HTML page with Python 3.5 & Selenium

I am trying to select all checkboxes that appear on an HTML page with Python 3.5 and Selenium. The final goal is obviously to manipulate them, but I can't even manage to select them properly.

I can't just give you the exact URL, because it's a special page on eBay and you need to be connected to see it. So, here's how to access the web page that interest me:

  1. Go to http://ift.tt/2hZWqXk
  2. Create an account or Log In
  3. Go to http://ift.tt/2i4an6y when connected
  4. Type in "iPhone" or any brand you want, it doesn't matter, and click on the "Start selling" button.
  5. You are on the page.

You can see that, if you type 'iPhone', eBay proposes you to choose from one of all of these categories:

Mobile Phones & Communication

Mobile & Smart Phones

Mobile Phone & PDA Accessories > Other Mobile Phone Accessories

Other Phones

Smart Watches

Mobile Phone & PDA Accessories > Cables & Adapters

Mobile Phone Parts

Mobile Phone & PDA Accessories > Chargers & Docks

Mobile Phone & PDA Accessories > Headsets

Mobile Phone & PDA Accessories > Cases & Covers

Wholesale & Job Lots

Other Wholesale & Job Lots

My goal is to tick the first checkbox, no matter how much checkboxes there are on the page. I just want to check the very first one. It's simple as that. In this example, that means check the checkbox "Mobile & Smart Phones".

I tried the XPATH method:

checkBox = driver.find_elements_by_xpath(".//input[@type='checkbox']")

But when I try print(checkBox), it returns [ ] (=nothing). It's strange because in Firefox this XPATH give me just exactly what I need.

I also tried to select it with the class name:

checkBox = driver.find_elements_by_class_name('inpChkBox')

But again, it results in nothing when I try to print it.

How can I just generate a list of all the checkboxes on the page, in order to manipulate them after?

It seems like the id, name, etc of all checkboxes are unique and generated automatically, so I can't just select them by their ID, or Value, or Name. I need something that detects all checkboxes in the page and put them in a list.

Thanks for any help




Aucun commentaire:

Enregistrer un commentaire