On gmx.net I want to click on a hyperlink in an email.
But the hyperlink is enclosed with two iframes.
How do I change into it?
My approach:
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class="app-stack_children--active"]')))
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class="js-box-flex need-overlay"]')))
As I suspected, your XPath is incorrect.
You have to either use
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class='app-stack__children l-vertical app-stack__children--active']')))
or
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[contains(#class, 'app-stack_children--active']')))
Obviously, the first option is better than the second one. There's always a chance that contains() will match more than one element. It is less probable if using equality.
Related
I'm trying to scrape a page whose classes are in this format class="jss262 jss434 jss263"
I tried to click on a box and insert a specific value but whether I use class, xpath or css selector, the element is not identified.
I believe I missed something but I'm not sure what it is. Is there a way to modify my code in order to access this element?
My code:
try:
button2 = WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.CLASS_NAME, 'jss262 jss434 jss263')))
button2.click()
button2.send_keys(att)
driver.implicitly_wait(5)
By.CLASS_NAME only accepts one class, use css_selector:
button2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.jss262.jss434.jss263')))
Im trying to perform select and click action from the search box result dropdown for testing purpose. Though i dont get ant error but i'm stuck and not able to do so, search results came then disappeared immediately. Please any one help me out. Im using Python script to automate webdriver. Here is the screenshot below for reference.
. I have tried webdriverwait for same action but it gives Timeout exception error. If there is any child actions from CSS to perform let me know. Here is what i tried
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.presence_of_element_located((By.XPATH, "//li[text()='flowers']")))
Apart from this snippet, i want to rather just perform enter key operation, where i get query result for 'flower' on this ecomm. website.
Here is the website URL- https://paytmmall.com
Once you type flower in the input field, there are multiple options appearing based on the input provided. They are in li tags and under b tag.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://paytmmall.com/")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.XPATH, "(//li)[4]/descendant::b[contains(text(),'flowers')]"))).click()
time.sleep is just for visibility purpose. you can remove that as well.
Also this xpath (//li)[4]/descendant::b[contains(text(),'flowers')] is based on xpath indexing , since I think you wanna select the 4th option which is flower itself. In case you wanna select a different option, you would have to write the different xpath.
In case you are looking to just select the searched item, it's better to pass enter key once you type flower in the input field.
You can use the below code for that :
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
search.send_keys(Keys.RETURN)
The suggested options are not containing the text directly in li elements, they are inside child elements inside li elements.
Try this instead:
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.visibility_of_element_located((By.XPATH, "//li//*[text()='flowers']")))
Xpath
I'm looking for Unique Xpath for this element (Select)
in google language settings
https://myaccount.google.com/language?hl=en
already have this xpath but I need something more accurate and unique
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH,
"//*[#id='yDmH0d']/div[11]/div/div[2]/div[3]/div[2]/button"))).click()
If you pay attention to the HTMLDOM :
You can construct a xpath based on attribute name only. You do not need their values nor their text.
//div[#data-is-touch-wrapper]/button[#data-id and #aria-label]
represent two matching nodes.
(//div[#data-is-touch-wrapper]/button[#data-id and #aria-label])[2]
should get the job done.
I would try to copy the X-Path from the Browser as discribed here:
Mozilla Help
My result would be: //*[#id="lang-selector"]
I'm having a lot of trouble finding the web element corresponding to the drop down box directly to the right of Code:. I've tried using Select, finding by tag_name, css_selector, xpath, name, class_name, select_by_visible_text, and haven't been able get it. I'm guessing the issue is that the dropdown box is nested within the form tag, highlighted in blue in the picture. The closest I've gotten is this:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#code_section_srch form#expertsearchformid select#expertsearchformid:toctextcodeid")))
Can you help me understand what I'm doing wrong?
Any insight would be greatly appreciated!
You have to use quotas, escape colon or use find by ID:
div#code_section_srch form#expertsearchformid select#expertsearchformid\:toctextcodeid
div#code_section_srch form#expertsearchformid select[id='expertsearchformid:toctextcodeid']
This should work:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "expertsearchformid:toctextcodeid")))
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[id='expertsearchformid:toctextcodeid']")))
I am using selenium in python and cannot get it to find and select the ok button. Here is the code I am trying to use that is not finding the button.
SubmitElem = driver.find_element_by_name('ctl00$PlaceHolderMain$ctl01$RptControls$btnOK')
SubmitElem.submit()
And this is the webpage code
<input name="ctl00$PlaceHolderMain$ctl01$RptControls$btnOK"
value="OK"
onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$PlaceHolderMain$ctl01$RptControls$btnOK", "", true, "", "", false, false))"
id="ctl00_PlaceHolderMain_ctl01_RptControls_btnOK" accesskey="o"
class="ms-ButtonHeightWidth" type="submit">
Any advice on what I am doing wrong? I found some suggestions to add onclick*= but it did not seem to work.
As noted earlier, if the element is in an iframe, you need to switch contexts. Additionally, if the element takes time to show up on the DOM, due to it being dynamically rendered by JavaScript, you might need waits. You basically want to wait until the element shows up on the page before performing actions on it.
From the Selenium docs:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
Here is the Answer to your Question:
As per the HTML you have provided, you may consider the following options:
You can consider to construct a xpath based on name as follows:
SubmitElem = driver.find_element_by_xpath("//input[#name='ctl00$PlaceHolderMain$ctl01$RptControls$btnOK']")
SubmitElem.submit()
You can also try a different xpath based on id as follows:
SubmitElem = driver.find_element_by_xpath("//input[#id='ctl00_PlaceHolderMain_ctl01_RptControls_btnOK']")
SubmitElem.submit()
Alternatively, you can also consider to call click() method instead of submit() method.
SubmitElem.click()
Again, it seems to me that the id and name is dynamic due to presence of JavaScript and AJAX Calls. In that case you can consider to construct a dynamic xpath as follows:
SubmitElem = driver.find_element_by_xpath("//input[starts-with(#id, 'ctl00')]")
SubmitElem.submit()
You can consider to construct a dynamic css_selector as follows:
SubmitElem = driver.find_element_by_css_selector("input[id^='ctl00']")
SubmitElem.submit()
Finally, if you see a NoSuchElementException or ElementNotVisibleException you may consider to induce some WebDriverWait as follows:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#name, 'ctl00')]")))
driver.find_element_by_css_selector("input[name^='ctl00']").click()
There are total 6 suggested locators to identify the element along with different other options.
Let me know if this Answers your Question.