Selenium WebDriver “find_element_by_xpath” on WebElement - python

<figcaption class="ncss-row">
<div class="ncss-col-sm-12 full">
<div class="figcaption-content">
<div class="copy-container ta-sm-c bg-white pt6-sm pb7-sm pb5-lg">
<h3 class="ncss-brand u-uppercase text-color-grey mb-1-sm mb0-md mb-3-lg fs12-sm fs14-md">
Air Jordan XI<!-- -->
</h3>
<h6 class="ncss-brand u-uppercase fs20-sm fs24-md fs28-lg">Black/Red<!-- -->
</h6>
</div>
<div data-qa="shop-now-cta" class="cta-container bg-white pt6-sm pb7-sm pb5-lg prl12-sm pb8-sm
pt8-lg ta-sm-c xh-highlight">
<div>
<button class="cta-btn u-uppercase ncss-btn text-color-white ncss-brand d-sm-b d-lg-ib pr5-
sm pl5-sm pt3-sm pb3-sm d-sm-ib bg-black test-buyable buyable-full-width buyable-feed"
data-qa="notify-me-cta theme-feed">通知我
</button>
</div>
</div>
</div>
</div>
</figcaption>
I want to locate the button by find_element_xpath, bu it always always appears
[<selenium.webdriver.remote.webelement.WebElement (session="10bf8a8d83d1b36188a6b1e41856c052", element="0.5294218443885808-2")>]
Any help would be greatly appreciated.
url = 'https://www.nike.com/cn/launch/'
browser = webdriver.Chrome()
browser.get(url)
elements = browser.find_element_by_xpath('//figure/div/div/figcaption/div/div/div[2]/div/button').text
print(elements)

To Click on the Button Induce WebDriverWait and wait for element_to_be_clickable() And use following Css selector.
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).click()
You need to import following libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
Edited
To get the Text of the button use below code.use get_attribute("textContent")
Or get_attribute("innerHTML") since the item text is hidden on the page.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
browser=webdriver.Chrome()
browser.get("https://www.nike.com/cn/launch/")
print(WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).get_attribute("textContent"))

Related

Unable to access the button in Selenium Python using Xpath

Error Msg:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}
I have tried with tagname as button too.
The HTML for this:
<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
autocomplete="off" method="post" target="_top" novalidate="novalidate">
<p class="username">To reset your password, enter your Salesforce username.</p>
<input type="hidden" name="locale" value="in" />
<div class="verifyform">
<label for="un" class="label">Username</label>
<input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
name="un" value="" autofocus />
<input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
<input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16" value="Cancel" >
</div>
<input type="hidden" name="lqs" value="locale=in" />
<div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
<a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
<p class="small">Video: Need Help Logging In?</p>
</form>
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service("C:\\Users\\Dell\\Desktop\\Selenium\\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()
The element Cancel doesn't have a innerText rather the value attribute is set as Cancel
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
Solution
To click element instead of presence_of_element_located() 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, "input[value='Cancel']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='Cancel']"))).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
Have you tried the following code:
driver.find_element(By.XPATH, '//*[#id="forgotPassForm"]/div[1]/input[3]').click()
To get the basic XPath you can inspect element by right clicking on the web page and selecting 'Elements'. You can then right click the button and then 'Copy > Copy XPath'. This will allow you to get the very basics working before then refactoring and using a more robust piece of xcode.
See screenshot:
The element you are trying to access is input element NOT button element.
Use following xpath to identify the element.
driver.find_element(By.XPATH, "//input[#name='cancel' and #value='Cancel']").click()
To handle dynamic element use WebDriverWait() and wait for element to be clickable.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='cancel' and #value='Cancel']"))).click()
You need to import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Click on item using Selenium and Python

I am trying to click on the "Training material statistics" by using the following code in Python, but it didn't work:
WebDriverWait(driver,20)\
.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="report-navigation"]/div[2]')))\
.click()
HTML:
<div id="report-navigation">
<div class="report-nav-btn active" onclick="Report.changeGrid(this, 'report-users-grid')">
User statistics
<div class="report-nav-arrow active"></div>
</div>
<div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-objects-grid')">
Training material statistics
<div class="report-nav-arrow"></div>
</div>
<div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-deliverables-grid')">
Learner assignments
<div class="report-nav-arrow"></div>
</div>
</div>
HTML Snapshot:
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, "div#report-navigation div[onclick*='report-objects-grid']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='report-navigation']//div[contains(#onclick, 'report-objects-grid')]"))).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

Python - Selenium Webdriver find an element where all the div nodes have similar names

I am trying to access a div where all divs have the same name. Let me explain. I am just starting out with selenium and python and I am trying to scrape a webpage just to learn. I ran into the following problem. I made the example html to show the buildup of the webpage. All the divs have the exact same class and title. Then there is the h1 tag for the item and the p tag for the color (which is a clickable link). I am trying to search a page when you give it certain instructions. Example: I am looking for a white racebike. I am able to find the bikes with the first line of code, but how do I find the right color within the racebike section? If I run the Python mentioned below I get an error message. Thanks in advance!
<!DOCTYPE html>
<html>
<body>
<div class=div title=example>
<h1>racebike</h1>
<p class='test'>black</p>
</div>
<div class=div title=example>
<h1>racebike</h1>
<p class='test'>white</p>
</div>
<div class=div title=example>
<h1>racebike</h1>
<p class='test'>yellow</p>
</div>
<div class=div title=example>
<h1>citybike</h1>
<p class='test'>yellow</p>
</div>
<div class=div title=example>
<h1>citybike</h1>
<p class='test'>green</p>
</div>
</body>
</html>
test = (self.driver.find_element_by_xpath("//*[contains(text(), racebike)]"))
test.self.driver.find_element_by_xpath(".//*[contains(text(), white)]").click
To locate/click() on the white racebike element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following xpath based Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h1[text()='racebike']//following-sibling::p[#class='test' and text()='white']"))).click()
Using XPATH considering the parent <div>:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='div' and #title='example']/h1[text()='racebike']//following-sibling::p[#class='test' and text()='white']"))).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
You can use same xpath which you tried in your solution. It might be possible server is taking too long to repond.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = WebDriverWait(page, 10).until(EC.presence_of_element_located((By.XPATH, "//p[contains(#class, 'white')]")))
element.click()
for multiple bikes with whiite color
elements= WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//p[contains(#class, 'white')]")))
for element in elements:
element.click()

How to click available button which refuses the click?

I'm working on automation of one website. My problem is that the button which is visible in my selenium (simply prints out) stopped working after recent update. Eventho I can see it, I cannot click it. I've noticed that it has started to have dynamic id selector, but class stays stable. What that can be ? Is there any other way to click it ?
<div>
<div>
<div class="pull-left middle-col-4">
<!---->
</div>
<div class="pull-left middle-col-4">
<!---->
</div>
</div>
<button tabindex="-1" id="exit-button-ZpyYaHCdmZ5jnmaamGhjaJjFcsVrmJOUcZWVaZlsaGlolpOaZg" class="btn btn-inverse btn-large pull-right">Wyjście</button>
</div>
To Click on the button induce WebDriverWait and element_to_be_clickable() And following locator strategy.
By Css Selector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.btn.btn-inverse.btn-large.pull-right"))).click()
By Xpath:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='btn btn-inverse btn-large pull-right' and starts-with(#id,'exit-button-')][text()='Wyjście']"))).click()
You need to import followings to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Please find below solution to click exit button if id is dynamic
1. Xpath and contains method
button=driver.find_element_by_xpath("//button[contains(text(), 'Wyjście')]")
button.click()
2. Class name
element = driver.find_element_by_class_name("btn btn-inverse btn-large pull-right")
element.click()

Xpath creation for Selenium for clicking a dropdown menu

I m trying to select a dropdown arrow menu after Status.
I'm trying to build a Xpath to click this :
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
Xpath = driver.find_element_by_xpath('//*[id="WIN_3_7"]/div/a').click()
But it says that the
Unable to locate an element
Below is the HTML snippet of the above image.
<div id="WIN_3_7" arid="7" artype="EnumSel" ardbn="Status" arlbox="0,4,104,17" class="df arfid7 ardbnStatus EnumSel" style="z-index:1928;top:173px; left:705px; width:262px; height:21px;" arwindowid="3">
<label id="label7" class="label f9" for="x-arid_WIN_3_7" style="top:4px; left:0px; width:104px; height:17px;">Status*</label><div class="selection" style="top:0px; left:105px; width:157px; height:21px;" arselmenu="[{v:"Assigned"},{v:"Pending"},{v:"Waiting Approval"},{v:"Planning"},{v:"In Progress"},{v:"Completed"},{v:"Rejected"},{v:"Cancelled",l:"Canceled"},{v:"Closed"}]">
<input id="arid_WIN_3_7" type="text" class="text " readonly="" style="top:0px; left:0px; width:136px; height:21px;" title="Assigned">
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="">
</a>
</div>
</div>
To click and expand the dropdown menu you can use the following line of code :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id="WIN_3_7"]/div[#class='selection']/a[#class='btn btn3d selectionbtn']/img[#class='btnimg']"))).click()
Update
As an alternative you can also try to click() on the <input> tag and expand the dropdown menu as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='text' and #id='arid_WIN_3_7']"))).click()

Categories