How to get text outside the div? - python

<div class="amlocator-store-information" style="" xpath="1">
<div class="amlocator-title" style="">The Better Health Store</div>
2420 E-Stadium Ann Arbor MI 48104
<br><br>
(613) 975-6613
<div style="" class="amasty_distance" id="amasty_distance_1">Distance:
<span class="amasty_distance_number"></span>
</div>
</div>
I am using selenium with python and trying to get the address from the DOM but I can't,
I have used
store_block_list = '//div[#class="amlocator-store-information"]'
store_block_list = '//div[#class="amlocator-title"]'
to capture the elements but cant get the address out as you can see the address is outside the //div element
Please note it is a list of elements and I then use for loop to loop around the element list

To extract the address i.e. 2420 E-Stadium Ann Arbor MI 48104 as it is a Text Node you need to induce WebDriverWait for the visibility_of_element_located() using execute_script() method and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(driver.execute_script('return arguments[0].childNodes[2].textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.amlocator-store-information")))).strip())
Using XPATH:
print(driver.execute_script('return arguments[0].childNodes[2].textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='amlocator-store-information']")))).strip())
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

Related

Fix wait time for Selenium Form using Python

I'm new to Python and am trying to automate a form fill up. I have done all the work but when trying to "Submit" it's not reading the XPATH and hence not clicking.
HTML:
<input style="text-align: center; height: 24px;" onclick="return (Button.OnClick(this, event));" onfocus="return (Button.OnFocus(this, event));" id="FormControl_V1_I1_B12" scriptclass="Button" class="t_cP1RLqWVMhs644yI_0 bg_cP1RLqWVMhs644yI_0 a4_cP1RLqWVMhs644yI_0" wrapped="true" direction="ltr" viewdatanode="13" formid="FormControl" originalid="V1_I1_B12" tabindex="0" title="" buttonid="CTRL10_5" value="Submit" type="button">
Here is the Python Code (the 2nd code handles the Ok popup):
click_value = web.find_element(By.XPATH('//*[#id="FormControl_V1_I1_B12"]')).get_attribute("onclick")
print(click_value)
web.switch_to.alert.accept()
Element snapshot:
Here is the HTML tag:
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, "input#FormControl_V1_I1_B12[formid='FormControl'][value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='FormControl_V1_I1_B12' and #formid='FormControl'][#value='Submit']"))).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

Fix Sign in error using Selenium in Python [duplicate]

I'm new to Python and am trying to automate a form fill up. I have done all the work but when trying to "Submit" it's not reading the XPATH and hence not clicking.
HTML:
<input style="text-align: center; height: 24px;" onclick="return (Button.OnClick(this, event));" onfocus="return (Button.OnFocus(this, event));" id="FormControl_V1_I1_B12" scriptclass="Button" class="t_cP1RLqWVMhs644yI_0 bg_cP1RLqWVMhs644yI_0 a4_cP1RLqWVMhs644yI_0" wrapped="true" direction="ltr" viewdatanode="13" formid="FormControl" originalid="V1_I1_B12" tabindex="0" title="" buttonid="CTRL10_5" value="Submit" type="button">
Here is the Python Code (the 2nd code handles the Ok popup):
click_value = web.find_element(By.XPATH('//*[#id="FormControl_V1_I1_B12"]')).get_attribute("onclick")
print(click_value)
web.switch_to.alert.accept()
Element snapshot:
Here is the HTML tag:
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, "input#FormControl_V1_I1_B12[formid='FormControl'][value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='FormControl_V1_I1_B12' and #formid='FormControl'][#value='Submit']"))).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

Selenium Webdriver to insert text from python in text area

<textarea style="font-size: 1.2em" ng-model="applications" ng-list=" " ng-trim="false" rows="15" cols="70" class="ng-valid ng-dirty ng-valid-parse ng-touched"></textarea>
This is the text area tag. I want to insert some text into the text area. How can I proceed with the python code for the selenium webdriver.
I tried:
driver.find_element_by_class_name("ng-pristine ng-valid ng-touched")
Please help me out.
To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("textarea.ng-valid.ng-dirty.ng-valid-parse.ng-touched[ng-model='applications']").send_keys("samhith gardas")
Using xpath:
driver.find_element_by_xpath("//textarea[#class='ng-valid ng-dirty ng-valid-parse ng-touched' and #ng-model='applications']").send_keys("samhith gardas")
However the desired element is a Angular element, so ideally to send a character sequence to 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, "textarea.ng-valid.ng-dirty.ng-valid-parse.ng-touched[ng-model='applications']"))).send_keys("samhith gardas")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[#class='ng-valid ng-dirty ng-valid-parse ng-touched' and #ng-model='applications']"))).send_keys("samhith gardas")
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
driver.find_element_by_class_name("ng-pristine.ng-valid.ng-touched").
Or
driver.find_element_by_xpath("//textarea[#claaa='ng-pristine ng-valid ng-touched']")
Class with space indicates multiple classes , so you have to replace space with dot in find_element_by_class
Else use xpath and find the element that matches absolute attribute value of the class

Problem clicking an element with selenium python

I'm trying to click an element on a web page using selenium and python
driver.find_element_by_class_name("market-selection.ng-scope").click()
But I get the error that the element isn't clickable
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
This is the element html (it's not in a frame btw); I'm guessing that the interactable part is the second div but I also tried the other two just in case...
<div class="market-selection-container 18" ng-repeat="market in wrapperCategoryGroup.currentMacroCategoria.mkl track by $index">
<!-- ngIf: wrapperCategoryGroup.marketTypes[market].nm -->
<div class="market-selection ng-scope" ng-if="wrapperCategoryGroup.marketTypes[market].nm" ng-class="{'active':wrapperCategoryGroup.currentMarketType == market}" ng-click="wrapperCategoryGroup.getMarketType(market)" style="">
<span class="ng-binding">
doppia chance
</span>
</div>
<!-- end ngIf: wrapperCategoryGroup.marketTypes[market].nm -->
</div>
Any hint?
To click on the element with text as save you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("div.market-selection.ng-scope > span.ng-binding").click()
Using xpath:
driver.find_element_by_xpath("//div[#class='market-selection ng-scope']/span[#class='ng-binding' and contains(., 'doppia chance')]").click()
Ideally, 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.market-selection.ng-scope > span.ng-binding"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='market-selection ng-scope']/span[#class='ng-binding' and contains(., 'doppia chance')]"))).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

How to print the text using a locator from the span in selenium webdriver python?

I am using the selenium for UI testing. I have below inspect element of chrome browser.
<div tabindex="-1" unselectable="on" role="gridcell" comp-id="2815" col-id="StartBaseMV" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value" style="width: 120px; left: 2020px; text-align: right; ">
<span>
<span class="ag-value-change-delta"></span>
<span class="ag-value-change-value">($5,281,158)</span>
</span>
</div>
What I tried for writing xpath.
//div[#col-id="StartBaseMV" and #class="ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value"]/span[#class="ag-value-change-data"]/span[#class="ag-value-change-value"]
But, it's not working. Suggest any clue
You were pretty close. Presumably you are trying to extract the text ($5,281,158) and to achieve that you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following solutions:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ag-cell.ag-cell-not-inline-editing.ag-cell-with-height.cell-number.ag-cell-value[col-id='StartBaseMV'] span.ag-value-change-value"))).get_attribute("innerHTML"))
Using XPATH:
print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[#col-id='StartBaseMV' and #class='ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value']//span[#class='ag-value-change-value']"))).get_attribute("innerHTML"))
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
As the data you want to fetch is stored as text, you can fetch it using text method like:
driver.find_element_by_class_name('ag-value-change-value').text
And if there are multiple elements with the same class name then you can use the xpath:
driver.find_element_by_xpath("//div[#col-id='StartBaseMV']//span[#class='ag-value-change-value']").text
You want to replace this line:
//div[#col-id="StartBaseMV" and #class="ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value"]/span[#class="ag-value-change-data"]/span[#class="ag-value-change-value"]
to this line:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#col-id='StartBaseMV']//span[#class='ag-value-change-value']")))
You Can Also Use Chrome Extension Chropath For Xpath Here is the link:
https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo
when you inspect and click on chropath you will see all xapths

Categories