Home > Mobile >  Click span class with selenium python
Click span class with selenium python

Time:09-28

I am using selenium and python to learn about automation web testing.

I want to click on the fivest or excelent button, while there is only span in it (I had learned that using id instead of span is so much easier) but in this case, I want to click the span.

I am using below code:

driver.find_element(By.XPATH, '//span[@]').click()
    driver.find_element(By.XPATH, '//span[@data-value="5"]').click()

there is five choice, very bad, bad, ok, good, excellent and i want to choose the excelent one, the picture of the five/excelent :

the button i want to click

here is the element :

the element

the class and the data-value change and depend of what we choose if is it excelent the class will be "ui_bubble_rating fl bubble_50" and the data-value will be "5" but if it is very bad it will be "ui_bubble_rating fl bubble_10" and the data-value will be "1"

Thank you for everyone who help me.

CodePudding user response:

This might help (from a Tripadvisor rating system):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains


chrome_options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=chrome_options)
browser.get('https://www.tripadvisor.in/UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html')
elem = browser.find_element(By.XPATH, '//span[@id="bubble_rating"]')

action = ActionChains(browser)
action.move_to_element_with_offset(elem, 50, 0).click().perform()

In that site, the class bubble_50 is added on mouse hover. However, I just offset the clicking. There might be other solutions out there. You can validate by checking the class and value (after the click)

  • Related