0

im trying to code a steambot in python that will post in steamgroup;Im having problem with last step, i cant find the sumbit button

<button type="submit" class="btn_green_white_innerfade btn_medium" id="commentthread_General_34191408_submit">
                                            <span>Post Discussion</span>
                                        </button>
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
perzsa
  • 25
  • 5

2 Answers2

2

To identify the submit button with text as Post Discussion you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = driver.find_element_by_css_selector("button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span")
    
  • Using XPATH:

    element = driver.find_element_by_xpath("//button[@class='btn_green_white_innerfade btn_medium' and starts-with(@id, 'commentthread_General_')]/span[text()='Post Discussion']")
    

However, as it is a submit button so moving ahead you will invoke click() on it, so you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn_green_white_innerfade btn_medium' and starts-with(@id, 'commentthread_General_')]/span[text()='Post Discussion']"))).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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
1

Here is your button, works 100%. I checked it locally.

driver.find_element(By.XPATH, "//button[@type='submit']")
IPolnik
  • 448
  • 2
  • 10
  • 2
    What do you mean, you checked it locally? Pasting HTML into an XPath helper tool and testing against it is great, but there's no way to guarantee 100% that this works without seeing the whole page. There may be multiple `button` elements with `submit` type, or the button may be hidden in an `iframe` in which case this would not work at all. – Christine Dec 11 '19 at 22:05
  • Yes, u r right. I'm always checking that Xpath is working using helper tool. When I'm saying 100% I mean, that Xpath is correct. I agree that, I can't guarantee, that its gonna work if there is other elements on the page with same attributes or iframes, but since in questions we have only this HTML, it,s should work. Thank u. – IPolnik Dec 12 '19 at 15:41
  • Your XPath also won't compile because you have `'submit'` in single quotes, and your XPath is also enclosed in single quotes. It should be changed to `"//button[@type='submit']"`. – Christine Dec 12 '19 at 15:45