I have been researching for a way to change the element of a web page and have yet found (or understood) how to do it. In the above element I want to be able to change numerical value of to another number that is represented by a placeholder (i.e. nod for number of draws. Or do I have to do it manually every time I run the program?).
My OS is Ubunt 18.04 and using latest python, selenium and pycharm IDE.
These are just a couple of the websites I have visited and I have visited a lot.
https://www.youtube.com/watch?v=tdgAIDR9snc
http://allselenium.info/javascript-using-python-selenium-webdriver/
Set value of input instead of sendKeys() - selenium webdriver nodejs
WEBELEMENT:
<select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
<option selected="selected" value="1"> Single Draw</option>
<option value="2"> 2 Consecutive</option>
<option value="3"> 3 Consecutive</option>
<option value="4"> 4 Consecutive</option>
<option value="5"> 5 Consecutive</option>
<option value="10">10 Consecutive</option>
<option value="20">20 Consecutive</option>
</select>
And my basic code is:
from Selenium import WebDriver
browser = webdriver.Chrome('/usr/bin/chromedriver')
page=browser.get("whatever address of whatever webpage interested in")
elem=browser.find_element_by_css_selector("#ctl00_MainContent_ddlRange")
browser.execute_script("arguments[0].setAttribute('<option selected="selected" value="20">','20 Consecutive</option>')", elem)
My goal is to change the value number in the first part of the option tag from:
<option selected="selected" value="1"> Single Draw</option>
to this:
<option selected="selected" value="44"> Single Draw</option>
In the execute_script I am always getting end of statement expected after the last ). I am also getting errors of ',' or ')' expected in the areas of "selected" and "20"
Also the css and xpath selectors to these are:
for <select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
selector = #ctl00_MainContent_ddlRange
xpath = //*[@id="ctl00_MainContent_ddlRange"]
for <option>
selector = #ctl00_MainContent_ddlRange > option:nth-child(1) (note that nth-child() can be 1 - 7)
xpath = //*[@id="ctl00_MainContent_ddlRange"]/option[1] (note that option[] can also be 1 - 7)
I have the webpage saved to disc and I can go into the html and change that one number then save the file and when I click on it works. I just want to be able to automate it.
Any help is much appreciated
Idzireit
EDIT:
So far this is the closest I have come to actually inserting the value I want. The new code is as follows:
browser.execute_script("""arguments[0].setAttribute
('select', '<option selected="selected" value="nod"> &snbsp;draws </option>')""", get_draws)
Results in this:
<option value="20" select="<option selected="selected" value="nod"> &nbsp;&snbsp;draws </option>">20 Consecutive</option>
The javascript I am trying to inject is getting injected in the middle of the element I am trying to modify (notice the option sequence repeats in the middle of the first bracket).
How can I correct this to make it look like this:
<option selected="selected" value="a number I am trying to change"> Draw<\option>
EDIT SOLVED:
Figured out how to change the value of an element. I had to change the search method of that element to this:
get_draws = browser.find_element_by_tag_name('option')
Then my next line was pretty simple:
browser.execute_script("""arguments[0].setAttribute('value', '100')""", get_draws)
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question.undetected Selenium– undetected Selenium2019年04月30日 08:14:29 +00:00Commented Apr 30, 2019 at 8:14
-
You need to escape the " " characters around the 'selected' and '20'.Mate Mrše– Mate Mrše2019年04月30日 08:16:50 +00:00Commented Apr 30, 2019 at 8:16
-
DebanjanB My apologies for rambling about ideas in other parts that I think have an affect on my code but simply put is how do it get the tag [option selected="selected" value="2"] to be [option selected="selected" value="34"] MateMrse not sure I understand what you mean by escaping the " " characters.idzireit– idzireit2019年04月30日 08:39:58 +00:00Commented Apr 30, 2019 at 8:39
-
@idzireit Update the question with the relevant HTML pleaseundetected Selenium– undetected Selenium2019年04月30日 08:48:27 +00:00Commented Apr 30, 2019 at 8:48
-
DebanjanB Hope this is edited correctly for you.idzireit– idzireit2019年04月30日 15:38:04 +00:00Commented Apr 30, 2019 at 15:38
1 Answer 1
For your second question, parametrizing the int value, try the following:
var = 100
js = f"arguments[0].setAttribute('value', '{var}')"
browser.execute_script(js, get_draws)
Just pass the value you want to the var
variable.
1 Comment
Explore related questions
See similar questions with these tags.