0

I am trying to click spam button with Selenium on Python. But when I go to three dotes on someone profile i can click report @thisperson then I can't click "It’s suspicious or spam" button. I'm going crazy because I tried every way for pulling button with selenium. For example, find_element_by_id, path, css selector, class name and They don't work. Please help me,show me a way, enlighten me. enter image description here My Python code for clicking button:

spam_button = browser.find_element_by_xpath('//form[@id="report_webview_form"]/button[2]/span')

here is html tags for this button : enter image description here

I tried a complicated path for clicking button :

spam_button = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')

I don't know how I write "document" tag. So I get the error. I tried find_element_by_id:

spam_button = browser.find_element_by_id('spam-btn')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()

And I got the error "Unable element" I am lost. How can I click the button with selenium ?

My whole code:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By


browser = webdriver.Chrome(ChromeDriverManager().install())
url ="https://twitter.com/ANYACCOUNT"

browser.get(url)
time.sleep(1)
browser.find_element_by_xpath("//a[@href='/login']").click()
time.sleep(1)
username = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input')
time.sleep(1)
username.send_keys("MYNICKNAME")
time.sleep(1)
password = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input')
time.sleep(1)
password.send_keys("MYPASSWORD")
time.sleep(1)
browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span').click()
time.sleep(1)
button = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/div/div/div/div/div/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(button).click(button).perform()
time.sleep(1)
sikayet = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div/div[2]/div/div/div/div[2]/div[3]/div/div/div/div[5]/div[2]/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(sikayet).click(sikayet).perform()
time.sleep(1)
#spam_button = browser.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')
#browser.find_element_by_xpath('//button[@id="spam-btn"]')
#browser.implicitly_wait(5)
#ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()
wait = WebDriverWait(browser,10 )
spam_button = wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
time.sleep(1)
spam_button.click()

I tried other ways as mentioned in below comment and I got this error everytime . enter image description here

1 Answer 1

0

Try using explicit wait instead of implicit:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
spam_button = browser.find_element_by_id('spam-btn')
spam_button.click()

OR,

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = browser.find_element_by_css_selector('#spam-btn>div')
spam_button.click()

Update: You need to switch to iframe.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://twitter.com/apiontkovsky/status/1385614545947820037')
wait = WebDriverWait(driver, 15)

# Authorise here
# ...

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg")))
driver.find_element_by_css_selector(".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Report')]")))
driver.find_element_by_xpath("//span[contains(text(),'Report')]").click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".r-1yadl64.r-16y2uox")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = driver.find_element_by_css_selector('#spam-btn>div')
spam_button.click()
Sign up to request clarification or add additional context in comments.

11 Comments

Thank u , I tried this codes but I got the error for more than 10 times.The error is TimeoutException: Message:
Can you copy the whole html code of the page and put it to the question?
I colud put the image , I couldn't whole code because it is so much
ok, i'll try myself. please describe test steps more clearly. which twit, which button to click?
ok. I will try and thanks so much .1 - describing libraries : import time from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.