clicking using selenium python but no response - python

I chose the xpath from here
This is the button I am trying to click
add =driver.find_element_by_xpath('//*[#id="root"]/div/main/div/section[4]/section/section[2]/section[2]/div[2]/div[1]/div/div/div[2]/div/div[2]/div')
add.click()
print("hey")
This is the code, add.click() does not generate any error and hey is printed but the button is not clicked..
Initially the add button is not in the viewport but when the code executes it automatically comes in the view port but nothing happens.
I have tried doing everything like scroll to element, and brought the element in the viewpoet but still nothing happens..
This is from where I got the xpath
<div class="sc-1usozeh-8 kTTqJP">
<span class="sc-1usozeh-6 fTsfFl">Add</span>
<i class="rbbb40-1 MxLSp sc-1usozeh-4 TZpZK" size="14" color="#ED5A6B">.
<svg xmlns="http://www.w3.org/2000/svg" fill="#ED5A6B" width="14"
height="14" viewBox="0 0 20 20" aria-labelledby="icon-svg-title-
icon-svg-desc-" role="img" class="rbbb40-0 hoSSCx">
<title>plus</title>
<path d="M15.5 9.42h-4.5v-4.5c0-0.56-0.44-1-1-1s-1 0.44-1 1v4.5h-
4.5c-0.56 0-1 0.44-1 1s0.44 1 1 1h4.5v4.5c0 0.54 0.44 1 1 1s1-0.46
1-1v-4.5h4.5c0.56 0 1-0.46 1-1s-0.44-1-1-1z"></path>
</svg>
</i>
</div>

The element is a dynamic element, so to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))).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
Update
As a last resort you can use execute_script() as follows:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))))

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']/.."))).click()
If above doesn't work try
button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']")))
driver.execute_script("arguments[0].scrollIntoView();", button)
action = ActionChains(driver)
action.move_to_element(button).click().perform()
You need to import action class:
from selenium.webdriver.common.action_chains import ActionChains

Related

How to click on the Instagram Comment button using Selenium in Python

I'm basically trying to write a bot which comments on the most recent post of my timeline.
I have issues with finding the comment button using selenium.
I have tried every way of finding it but still didnt succeed.
HTML:
<svg aria-label="Kommentar" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 24 24" width="24"><path d="M20.656 17.008a9.993 9.993 0 10-3.59 3.615L22 22z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="2"></path></svg>
Code trials:
driver.find_element_by_xpath('/html/body/div\[5\]/div\[2\]/div/article/div\[3\]/section\[3\]/div/form/textarea').click()
Anyone can help me here?
The click on the Comment button Kommentar within Instagram you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg[aria-label='Kommentar']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and #aria-label='Kommentar']"))).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
Just use ENTER method:
from selenium.webdriver.common.keys import Keys
addComment = find_element_by_xpath('/html/body/div\[5\]/div\[2\]/div/article/div\[3\]/section\[3\]/div/form/textarea')
addComment.send_keys(Keys.ENTER)

Click a button to display a text box in python selenium

I am trying to click a button which un-hides another text box. The button click changes the script from
<span id="incident.u_brand_edit" style="display: none;">
to
<span id="incident.u_brand_edit" style>
Following is the button HTML
<button class="btn btn-default btn-ref" type="button" aria-labelledby="incident.u_brand_title_text"
data-target="#incident\.u_brand" title="" tabindex="0" id="incident.u_brand_unlock"
data-type="glide_list_unlock" data-ref="incident.u_brand" data-auto-close="false"
data-placement="auto" style="margin-right: 5px;" list-read-only="false"
data-original-title="Unlock Brand"><span class="icon icon-locked" aria-hidden="true"></span></button>
I am trying to achieve this using the following code
driver.find_element_by_id("incident.u_brand_unlock").click()
Also tried this
driver.find_element_by_id("incident.u_brand_unlock").send_keys("\n")
The above codes are focusing on the button but it's not clicking and the text box is not unhiding to perform further operations.
Try to use the ActionChains class in your code like below -
# Import
from selenium.webdriver import ActionChains
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
Button_Click = wait.until(EC.element_to_be_clickable((By.ID, 'incident.u_brand_unlock')))
action.move_to_element(Button_Click).click().perform()
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default.btn-ref[id$='u_brand_unlock'][data-original-title='Unlock Brand']>span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default btn-ref' and contains(#id, 'u_brand_unlock')][#data-original-title='Unlock Brand']/span"))).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
Found out the answer in this post . Had to remove the style attribute of the field I wanted to appear.

How to scroll a web page down until specific button to click?

I am working with Python and Selenium but I got stuck scraping a page trying to scroll down until selenium finds the classic "Show more" button (that seems to be hidden).
What I have done 'til now is the following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
element = browser.find_element_by_xpath('//*[#id="ember371"]/button') # you can use ANY way to locate element
coordinates = element.location_once_scrolled_into_view # returns dict of X, Y coordinates
browser.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))
However, whatever I try to do Python throws me several kind of errors, for example the below.
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="ember371"]/button"}
(Session info: chrome=80.0.3987.122)
Could it be due to the fact that the button is hidden?
Below is the HTML code for the button I am looking for and that I would like to click on.
<button class="pv-profile-section__card-action-bar pv-skills-section__additional-skills artdeco-container-card-action-bar artdeco-button artdeco-button--tertiary artdeco-button--3 artdeco-button--fluid" aria-controls="skill-categories-expanded" aria-expanded="false" data-control-name="skill_details" data-ember-action="" data-ember-action-2182="2182">
<span aria-hidden="true">
Show more
</span>
<span class="visually-hidden">
BLA BLA BLA
</span>
<li-icon aria-hidden="true" type="chevron-down-icon" class="pv-skills-section__chevron-icon" size="small"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" data-supported-dps="16x16" fill="currentColor" width="16" height="16" focusable="false">
<path d="M8 9l5.93-4L15 6.54l-6.15 4.2a1.5 1.5 0 01-1.69 0L1 6.54 2.07 5z"></path>
</svg></li-icon>
</button>
#################################################################################
It worked with the following:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# scroll down smoothly
scheight = .1
while scheight < 1.0:
browser.execute_script("window.scrollTo(0, document.body.scrollHeight*%s);" % scheight)
scheight += .1
time.sleep(1)
try:
browser.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//button[#aria-controls='skill-categories-expanded' and #data-control-name='skill_details']/span[normalize-space()='Show more']"))))
browser.execute_script("arguments[0].click();", WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-controls='skill-categories-expanded' and #data-control-name='skill_details']/span[normalize-space()='Show more']"))))
except:
print('NO')
The element Show more within linkedin website is EmberJS enabled element. So to scrollIntoView the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[aria-controls='skill-categories-expanded'][data-control-name='skill_details']>span"))))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-controls='skill-categories-expanded'][data-control-name='skill_details']>span"))))
Using XPATH:
driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//button[#aria-controls='skill-categories-expanded' and #data-control-name='skill_details']/span[normalize-space()='Show more']"))))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-controls='skill-categories-expanded' and #data-control-name='skill_details']/span[normalize-space()='Show more']"))))
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
You can try using a while loop here, check for the existence of the button as the condition, and put the browser.execute_script in the body. In this way, you would start by checking if the button exists, if not then scroll for 200 px or so (just make sure you don't scroll past the button) until the button appears.
You can first try to find if the "show more" button is enabled by using the isEnabled() method. This method returns “true” value if the specified web element is enabled on the web page otherwise returns “false” value if the web element is disabled on the web page.
If isEnabled() method returns False, you can then try scrolling down the page till the element is visible and use the isEnabled() method again.
Also, try replacing id with XPath. It may work.

How to click on a button with in a submenu with selenium in python?

I have a webpage, and within it there is a menu that looks like this:
<span localization="" data-key="EXPORT_AND_IMPORT" class="ng-binding ng-isolate-scope">Export & Import</span>
when I click it, it opens a sub menu with three options:
<div class="qmenu dropdown-menu positioned" style="top: 293px; left: 900px; max-height: 99%; width: 231px; height: 113px;"><ul class="menu-items"><li class="no-check"><a><i class="icon icon-download-lg"></i>Export Data...</a></li><li class="no-check"><a><i class="icon icon-uploadcsv-lg"></i>Import Data...</a></li><li class="divider"></li><li class="no-check"><a><i class="icon icon-twofiles"></i>Manage Previous Downloads...</a></li></ul></div>
here is a picture:
How can I press the Export & Import button and then the Export Data button with in the submenu?
First to click() on the element with text as Export & Import and then to click() on the submenu element with text as Export Data... you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ng-binding.ng-isolate-scope[data-key='EXPORT_AND_IMPORT']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.qmenu.dropdown-menu>ul.menu-items li:nth-of-type(1)>a"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ng-binding ng-isolate-scope' and #data-key='EXPORT_AND_IMPORT']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='qmenu dropdown-menu positioned']/ul[#class='menu-items']//li/a[contains(., 'Export Data')]"))).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

Unable to click on Button using Selenium Python

I'm trying to click on this button to go to the next page, but for some reason, I'm unable to. I tried xpath, css, and class selectors as well as the data-trekkie-id attribute, but nothing I have tried worked. Any help? Code below:
<div class="step__footer" data-step-footer="">
<button name="button" type="submit" class="step__footer__continue-btn btn " data-trekkie-id="continue_to_shipping_method_button" aria-busy="false">
<span class="btn__content">
Continue to shipping method
</span>
</button>
</div>
As per the HTML to invoke click() method on the button with text as Continue to shipping method you need to induce WebDriverWait 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.step__footer__continue-btn.btn[data-trekkie-id='continue_to_shipping_method_button']>span.btn__content"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='step__footer__continue-btn btn' and #data-trekkie-id='continue_to_shipping_method_button']/span[#class='btn__content']"))).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
This should work:
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(#class, 'step__footer__continue-btn')]").click()

Categories

Resources