0

I'm trying to get the value of an id that has space in between, using selenium. The HTML looks like this

    <div id="buyAlchemy lab" onclick="Buy('Alchemy lab');" style="background-image:url(labicon.png);" class="grayed">Turns gold into cookies!</div>

How can I select this id's text?

If I do

    driver.find_element_by_css_selector("div #buyAlchemy lab").text

I'm getting an error which says

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div #buyAlchemy lab"}
     
  • You can use xpath like '//div[id="buyAlchemy lab"]' instead of id in this case or use css selector div[id='buyAlchemy lab'] – Sreenivasulu Aug 05 '21 at 14:08
  • have you tried to simply do `find_element_by_xpath` [look here](https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome), or maybe have a time.sleep() to make sure your element has propely loaded to the screen. – Buddy Bob Aug 05 '21 at 14:08

1 Answers1

0

You can use id:

driver.find_element_by_id("buyAlchemy lab").text

or xpath:

driver.find_element_by_xpath("//div[@id='buyAlchemy lab']").text
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • driver.find_element_by_id("buyAlchemy lab").text This worked!. Thanks a bunch!! – Shoto Venkatesh Aug 05 '21 at 14:25
  • You are welcome! XPath should be work too. As about the CSS selector it's interesting. I'm trying to find the solution with CSS selector too. – Prophet Aug 05 '21 at 14:28
  • can you try this and let me know if that worked? `driver.find_element_by_css_selector("div#buyAlchemy#lab").text` ? – Prophet Aug 05 '21 at 14:45
  • So, that means `div#buyAlchemy#lab` is not the correct CSS locator here. Thanks! I will try to learn about this! – Prophet Aug 05 '21 at 17:24
  • Moment, but this should work: `div[id='buyAlchemy lab']` right? – Prophet Aug 05 '21 at 18:03
  • 1
    Not sure which method to pass it on, but was just going through this [link](https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html). When I had something like this `
    Clicks automatically!
    `, getting the value was not an issue
    – Shoto Venkatesh Aug 05 '21 at 18:32