0

I am trying to access a 2nd or 3rd Checkbox Element that have the same class name and tag..Each of the checkbox is enclosed in a separate div and the div also has the same class name. I previously searched on stackoverflow and Google and tried some of the following but none of them is working

driver.find_element_by_xpath("(//div)[@class='classname value'])[2]")
driver.find_element_by_xpath("(//div[@class='classname value']) [position()=2]")  

Also

driver.find_element_by_xpath("(//span)[@class='classname value'])[2]")
driver.find_element_by_xpath("(//span[@class='classname value']) [position()=2]")

This is somehow the elements look like by inspection

<div class='a checkbox'>
 <label><input type='checkbox'><span class='b'>Paid</span></label>
</div>
<div class='a checkbox'>
 <label><input type='checkbox'><span class='b'>Free</span></label>
</div> 

I basically want to access the checkbox with Free Text..My Overall Code is this

from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='All Filters']"))).click()
time.sleep(10)
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal-body']//label[.//span[@data-purpose='filter-option-title' and text()='Free']]/input[@type='checkbox']"))).click()
Joe Ann
  • 15
  • 6

4 Answers4

2

You may use contain If the DOM only contain only one Free text as shown below example.

driver.find_elements_by_xpath("//*[contains(text(), 'Free')]")

If the DOM contain multiple Free text, then you need to pass it to List and based on the text position you need to iterate the List.

stacktome
  • 790
  • 2
  • 9
  • 32
  • @JoeAnn Check how many free text in your DOM, if you have more than one pass it to list and access based on the text position. Above example only find the elements it will not do any action, you need to call the action (Click) – stacktome Sep 30 '19 at 14:01
  • I only have one Free text in my DOM Basically i am trying to access the Free checkbox from this page udemy.com/courses/search/?src=ukw&q=python&p=1 click All Filter> Free Checkbox – Joe Ann Sep 30 '19 at 14:06
  • i have updated the question by including the whole code that i have at the moment – Joe Ann Sep 30 '19 at 15:52
0

Amend your code to be looking like this:

from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[.//span[text()='All Filters']]"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//label[@title and .//span/span[text()='Free']]"))).click()
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • Hey this is not working as i mentioned in my question as well – Joe Ann Sep 30 '19 at 13:48
  • Have you checked my test? How is it different from your real case? I took your example. – Alexey R. Sep 30 '19 at 13:56
  • Yeah i checked....Basically i am trying to access the Free checkbox from this page https://www.udemy.com/courses/search/?src=ukw&q=python&p=1 click All Filter> Free Checkbox – Joe Ann Sep 30 '19 at 14:02
  • As you can see from the tool I tested the solution in, the xPath actually locates the check-box that you require. If it is not working for you, the issue might be not in just xpath. – Alexey R. Sep 30 '19 at 14:04
  • I tried it but this it throws an error of 'No Such Element'. So when i click on the All Filter Option on DOM, it opens a big menu...From there i need to select that Free text so is that menu then the part of DOM once clicked right? – Joe Ann Sep 30 '19 at 14:44
  • Right. Since there is JavaScript addressing UI changes, the pane might open too fast. Try to wait for the element. – Alexey R. Sep 30 '19 at 15:06
  • I waited for a long time even included this statement time.sleep(30) but still it is unable to check the checkbox – Joe Ann Sep 30 '19 at 15:10
  • can you update your question with the code that you have by the moment? – Alexey R. Sep 30 '19 at 15:15
  • Check my second update. The issue might be that you're locating `span` but not a `button`. The xpath that you use to locate check-box then does not work for me at all even when the pane is open and I'm testing manually in dev tools. – Alexey R. Sep 30 '19 at 15:33
  • Thank You...Can you please provide the complete code that you have used while locating that checkbox? – Joe Ann Sep 30 '19 at 15:36
  • OK...Have you tried it on the browser..Cause i checked it now and this time it throws the following error raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view – Joe Ann Sep 30 '19 at 15:43
  • This is beacuse your screen resolution is not big enough and your checkbox is out of view port. Try the solution with scroll. I have updated the code. – Alexey R. Sep 30 '19 at 15:49
  • It now says that ElementNotInteractableException: Message: Element could not be scrolled into view – Joe Ann Sep 30 '19 at 15:58
  • Yes...Finally it worked...Thank You So Much for your Hugh Help Can you now please one last time explain, how you located these element...That Xpath part specially? – Joe Ann Sep 30 '19 at 17:23
  • It is alsways a matter of trials. Picking the proper xpath for UI is not the same as for just an XML document since even if you can locate the element in DOM, that element might not receive your interaction attempt because of CSS issues or JS issues. I always start from the very specific xpath that I would use in pure XML. Then if it does not work I try the elements one or two levels up and one or two levels down the tree, – Alexey R. Sep 30 '19 at 17:36
  • That is Awesome bro.. Thank You so much – Joe Ann Sep 30 '19 at 17:42
  • Just mark as correct. It is reputation-independent action. – Alexey R. Oct 01 '19 at 14:24
0

Try the following xpath to click on Free checkbox on udemy account.

 //div[@class='modal-body']//label[.//span[@data-purpose='filter-option-title' and text()='Free']]/input[@type='checkbox']

Induce WebDriverWait and element_to_be_clickable() And Following XPATH locator.

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal-body']//label[.//span[@data-purpose='filter-option-title' and text()='Free']]/input[@type='checkbox']"))).click()

Here is the code.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Chrome("path of the chrome driver")
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='All Filters']"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal-body']//label[.//span[@data-purpose='filter-option-title' and text()='Free']]/input[@type='checkbox']"))).click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • I tried it but this it throws an error of 'No Such Element'. So when i click on the All Filter Option on DOM, it opens a big menu...From there i need to select that Free text so is that menu then the part of DOM once clicked right? – Joe Ann Sep 30 '19 at 14:45
  • @JoeAnn : I have updated the complete code.try and let me know.If you get error then post the entire error. – KunduK Sep 30 '19 at 14:48
  • Yes i got an error once again but this time it throws this error...Here it is raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Joe Ann Sep 30 '19 at 15:00
  • @JoeAnn : where are you getting error to click on `checkbox` or `All filters`? – KunduK Sep 30 '19 at 15:05
  • The 'All filter' is clicked successfully but the checkbox can't be located – Joe Ann Sep 30 '19 at 15:06
0

As it is a <input> element presumably moving ahead you need to invoke click() on the element with respect to the text Free, you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH 1:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='a checkbox']//label//span[@class='b' and text()='Free']//preceding::input[1]"))).click()
    
  • Using XPATH 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='b' and text()='Free']//preceding::input[1]"))).click()
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried it but this it throws an error of 'No Such Element'. So when i click on the All Filter Option on DOM, it opens a big menu...From there i need to select that Free text so is that menu then the part of DOM once clicked right? – Joe Ann Sep 30 '19 at 14:43
  • @JoeAnn I don't have visibility of the `All Filter Option` element. So once the menu opens use the line of code to click on the element with text as `Free`. – undetected Selenium Sep 30 '19 at 14:46
  • i have updated the question by including the whole code that i have at the moment – Joe Ann Sep 30 '19 at 15:30