0 votes
1 view
in DevOps and Agile by (19.8k points)

I have the below code that clicks on an element to pop up a screen and copy the text in it

el1 = driver.find_element_by_id("keyDev-A")

el1.click()

el2 = driver.find_element_by_class_name("content")

print(el2.text)

However, when I tried to get selenium to click on the button within that popup with

el3 = driver.find_element(By.CLASS_NAME, "action-btn cancel alert-display")

el3.click()

It produces an error message: invalid selector: Compound class names not permitted

This is the HTML that I am trying to get selenium to click on. The Close button.

<div class="nav">

    <span class="action-btn confirm prompt-display">Confirm</span>

    <span class="action-btn cancel prompt-display">Cancel</span>

    <span class="action-btn cancel alert-display">Close</span>

</div>

How should I be writing el3 in order to click on the close button?

2 Answers

0 votes
by (63.4k points)

Compound class names are no longer supported.

What you may do instead is attempt using CSS selectors.

In your case, the following line of code should help you get the element you want :

el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")

It finds the element with all three classes (action-btn, cancel and alert-display) in the class attribute.

Do note that the order of the classes doesn't matter here and any of the classes might appear anywhere within the class attribute.

As long as the element has all 3 classes, it'll be selected.

If you want the order of the classes to be fixed, you can use the following Xpath:

el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']")

I hope this resolves the issue!

0 votes
by (33.2k points)

Instead of using the Class name you can use the xpath of the element it works fine for me.

For example:

driver.get("https://www.coupondunia.in/");

driver.findElement(By.id("header-search-input")).sendKeys("Paytm",Keys.ENTER);

Thread.sleep(3000);

driver.findElement(By.xpath("/html[1]/body[1]/div[8]/div[17]/div[1]/div[2]/div[2]/div[2]/ul[1]/li[3]")).click();

Hope this answer helps you!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...